<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>蓝湖畔淅淅沥沥的雨</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="蓝湖畔淅淅沥沥的雨">
<meta property="og:url" content="http://example.com/index.html">
<meta property="og:site_name" content="蓝湖畔淅淅沥沥的雨">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="StarsWhisper">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="蓝湖畔淅淅沥沥的雨" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/plugin/bganimation/bg.css">

  

  <link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css" rel="stylesheet" type="text/css">
<meta name="generator" content="Hexo 6.1.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <div class="widget-wrap mobile-header">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

        <section id="main">
  
    <article id="post-求生之路2zonemod药抗插件服务器搭建" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/06/11/%E6%B1%82%E7%94%9F%E4%B9%8B%E8%B7%AF2zonemod%E8%8D%AF%E6%8A%97%E6%8F%92%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BA/" class="article-date">
  <time class="post-time" datetime="2022-06-11T14:06:36.000Z" itemprop="datePublished">
    <span class="post-month">6月</span><br/>
    <span class="post-day">11</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/06/11/%E6%B1%82%E7%94%9F%E4%B9%8B%E8%B7%AF2zonemod%E8%8D%AF%E6%8A%97%E6%8F%92%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BA/">求生之路2zonemod药抗插件服务器搭建</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/">教练我想学挂边躲牛</a>,<a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/%E6%B8%B8%E6%88%8F/">游戏</a>,<a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/%E6%B8%B8%E6%88%8F/%E6%B1%82%E7%94%9F%E4%B9%8B%E8%B7%AF2/">求生之路2</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="前言"><a class="markdownIt-Anchor" href="#前言"></a> 前言</h1>
<p>以前曾经写过一篇关于开服的博客，但是现在尝试开服的时候发现老博客的一些内容已经不管用了，而且老博客在转移的过程中，格式出了写问题，所以写一个新的。</p>
<h1 id="普通l4d2服务器的搭建"><a class="markdownIt-Anchor" href="#普通l4d2服务器的搭建"></a> 普通L4D2服务器的搭建</h1>
<p>主要还是搬sir大佬的文章咯</p>
<h2 id="环境和必要组件"><a class="markdownIt-Anchor" href="#环境和必要组件"></a> 环境和必要组件</h2>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">dpkg --add-architecture i386 # enable multi-arch</span><br><span class="line">apt-get update &amp;&amp; apt-get upgrade</span><br><span class="line">apt-get install libc6:i386 # install base 32bit libraries</span><br><span class="line">apt-get install lib32z1</span><br><span class="line">apt-get install screen</span><br></pre></td></tr></table></figure>
<h2 id="创建一个名为steam的账户"><a class="markdownIt-Anchor" href="#创建一个名为steam的账户"></a> 创建一个名为steam的账户</h2>
<p>这个用户名是可以随意指定的，但sir大佬的教程里用的steam，<s>如果我们也用steam的话就不用手动改一些路径了(什么落伍的老方法，今非昔比了)</s>。</p>
<p>如果你不适用steam作为账户名，只需要修改SRCDS_USER变量名就好了(后面会说)。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">adduser steam</span><br><span class="line">adduser steam sudo</span><br><span class="line">login</span><br></pre></td></tr></table></figure>
<h2 id="安装steamcmd和求生之路2游戏"><a class="markdownIt-Anchor" href="#安装steamcmd和求生之路2游戏"></a> 安装steamcmd和求生之路2游戏</h2>
<p>这是sir大佬的教学文档里写的。主要做了: 安装steamcmd 匿名登录  更改安装路径 安装求生之路2。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">(先进入steam账户的根目录)</span><br><span class="line">wget http://media.steampowered.com/installer/steamcmd_linux.tar.gz</span><br><span class="line">tar -xvzf steamcmd_linux.tar.gz</span><br><span class="line">./steamcmd.sh</span><br><span class="line">login anonymous</span><br><span class="line">force_install_dir ./Steam/steamapps/common/l4d2</span><br><span class="line">app_update 222860 validate</span><br><span class="line">quit</span><br></pre></td></tr></table></figure>
<p><code>(2022/06/11)</code><a target="_blank" rel="noopener" href="http://xn--xxxsteam-c49lx0g2nv5fc51f57im2aq1bk26atu0e6nf.so">但我用的时候会报打不开xxxsteam.so</a>(忘记名字了)库的错误，错误原因未知，我猜测是steamcmd的版本过时了，所以找到了steamcmd的文档<a target="_blank" rel="noopener" href="https://developer.valvesoftware.com/wiki/SteamCMD#Downloading_SteamCMD">SteamCMD - Valve Developer Community (valvesoftware.com)</a>，发现可以直接apt安装(Ubuntu)， 由于运行steamcmd，会自动创建在当前目录创建<code>.steam</code>目录，而steamcmd的工作目录在<code>./.steam/steamcmd</code>，所以路径想和sir保持一致偷懒的话，路径要修改。</p>
<p>所以流程变成了:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">(先进入steam账户根目录)</span><br><span class="line">apt install steamcmd</span><br><span class="line">steamcmd</span><br><span class="line">force_install_dir ../../Steam/steamapps/common/l4d2</span><br><span class="line">login anonymous</span><br><span class="line">app_update 222860 validate</span><br><span class="line">quit</span><br></pre></td></tr></table></figure>
<h2 id="开启无插件的本地服务器"><a class="markdownIt-Anchor" href="#开启无插件的本地服务器"></a> 开启无插件的本地服务器</h2>
<p>安装完求生之路2之后，就可以开求生服务器了，但现在还是官方版本。</p>
<p>运行<code>/home/steam/Steam/steamapps/common/l4d2</code>目录的 <code>srcds_run</code>脚本直接开服即可</p>
<h1 id="zonemod药抗插件的安装"><a class="markdownIt-Anchor" href="#zonemod药抗插件的安装"></a> ZoneMod药抗插件的安装</h1>
<h2 id="插件安装"><a class="markdownIt-Anchor" href="#插件安装"></a> 插件安装</h2>
<p>把sir大佬的项目克隆到相应位置就安装好了。</p>
<p><a target="_blank" rel="noopener" href="https://github.com/SirPlease/L4D2-Competitive-Rework">SirPlease/L4D2-Competitive-Rework: Just refreshing and optimizing the core files a bit, eh? (github.com)</a></p>
<h2 id="基本配置"><a class="markdownIt-Anchor" href="#基本配置"></a> 基本配置</h2>
<h4 id="cfgservercfg"><a class="markdownIt-Anchor" href="#cfgservercfg"></a> ./cfg/server.cfg</h4>
<p>服务器名称，组号，设置为只有组内成员能搜索到。你可以把这个文件改名为<code>server1.cfg</code>(后面会说)。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hostname &quot;蓝湖畔淅淅沥沥的雨&quot; </span><br><span class="line">sv_steamgroup &quot;41174273&quot;</span><br><span class="line">sv_steamgroup_exclusive &quot;1&quot; </span><br></pre></td></tr></table></figure>
<h4 id="dedicated-server-install-guidesrcds1"><a class="markdownIt-Anchor" href="#dedicated-server-install-guidesrcds1"></a> ./Dedicated Server Install Guide/srcds1</h4>
<p>修改路径，以及配置IP端口、游戏信息等内容。</p>
<p>这个文件需要被放在<code>/etc/init.d/</code>目录下。</p>
<p>注意PARAMS变量中的<code>server$SVNUM.cfg&quot;</code>，这说明你的<code>./cfg/</code>路径下要有<code>server1.cfg</code>文件。当然你可以定义多种配置，写多个启动脚本。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">SRCDS_USER=&quot;steam&quot;</span><br><span class="line"></span><br><span class="line">SVNUM=1</span><br><span class="line">IP=0.0.0.0</span><br><span class="line">PORT=27015</span><br><span class="line">NAME=L4D2_Server$SVNUM</span><br><span class="line">PARAMS=&quot;-game left4dead2 -ip $IP -port $PORT +sv_clockcorrection_msecs 25 -timeout 10 -tickrate 100 +map c2m1_highway -maxplayers 12 +servercfgfile server$SVNUM.cfg&quot;</span><br></pre></td></tr></table></figure>
<h2 id="更加方便"><a class="markdownIt-Anchor" href="#更加方便"></a> 更加方便</h2>
<p>编写shell脚本来调用脚本srcds~~(什么牛马套娃)~~</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">if [ $2 = &quot;start&quot; ]</span><br><span class="line">then</span><br><span class="line">/etc/init.d/srcds$1 &quot;start&quot;</span><br><span class="line">elif [ $2 = &quot;stop&quot; ]</span><br><span class="line">then</span><br><span class="line">/etc/init.d/srcds$1 &quot;stop&quot;</span><br><span class="line">elif [ $2 = &quot;restart&quot; ]</span><br><span class="line">then</span><br><span class="line">/etc/init.d/srcds$1 &quot;restart&quot;</span><br><span class="line">else</span><br><span class="line">echo &quot;Unknow command. arg should be [start/stop/restart]&quot;</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
<p>示例</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">./startgame.sh 2 restart</span><br><span class="line">相当于</span><br><span class="line">/etc/init.d/srcds2 restart</span><br></pre></td></tr></table></figure>
<h1 id="服务器配置"><a class="markdownIt-Anchor" href="#服务器配置"></a> 服务器配置</h1>
<h2 id="服务器管理员"><a class="markdownIt-Anchor" href="#服务器管理员"></a> 服务器管理员</h2>
<p>在<code>addons\sourcemod\configs\core.cfg</code>文件中修改PassInfoVar变量的值，它声明了表示密码的变量的名称， 这个值可以随意更改，也可以不改用sir写的默认值。假设我们改成&quot;_abcd&quot;</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;PassInfoVar&quot; &quot;_abcd&quot;</span><br></pre></td></tr></table></figure>
<p>在<code>addons\sourcemod\configs\admins_simple.ini</code>文件中，拉到最下面加入以下内容。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&quot;名称1&quot; &quot;99:z&quot; &quot;密码1&quot;</span><br><span class="line">&quot;名称2&quot; &quot;99:z&quot; &quot;密码2&quot;</span><br></pre></td></tr></table></figure>
<p>这时候名称1和名称2已经被设为管理员了，如果想使用这两个名称登录，就需要在你的信息中设置对应的密码。</p>
<p>如&quot;名称1&quot;想进入服务器的话，需要先在控制台设置:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">setinfo &quot;_abcd&quot; &quot;密码1&quot;</span><br></pre></td></tr></table></figure>
<p>如&quot;名称2&quot;想进入服务器的话，需要先在控制台设置:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">setinfo &quot;_abcd&quot; &quot;密码2&quot;</span><br></pre></td></tr></table></figure>
<p>否则， 进不去，进去了再改成管理员名称也会被题出来。</p>
<h2 id="游戏参数配置-只写改最新的zonemod插件"><a class="markdownIt-Anchor" href="#游戏参数配置-只写改最新的zonemod插件"></a> 游戏参数配置 (只写改最新的zonemod插件)</h2>
<h4 id="限制连推次数"><a class="markdownIt-Anchor" href="#限制连推次数"></a> 限制连推次数</h4>
<p>路径：<code>./cfg/cfgogl/zonemod/shared_cvars.cfg</code></p>
<blockquote>
<p>confogl_addcvar z_gun_swing_vs_min_penalty 10<br />
confogl_addcvar z_gun_swing_vs_max_penalty 15</p>
</blockquote>
<h2 id="ai特感"><a class="markdownIt-Anchor" href="#ai特感"></a> AI特感</h2>
<p>路径：<code>./cfg/cfgogl/zonemod/shared_cvars.cfg</code></p>
<p>阻碍ai特感刷新</p>
<blockquote>
<p>confogl_addcvar confogl_blockinfectedbots “0”</p>
</blockquote>
<p>导演系统允许ai特感刷新</p>
<blockquote>
<p>confogl_addcvar director_allow_infected_bots “1”</p>
</blockquote>
<h2 id="安全屋外止疼药数量"><a class="markdownIt-Anchor" href="#安全屋外止疼药数量"></a> 安全屋外止疼药数量</h2>
<p>路径：<code>./cfg/cfgogl/zonemod/confogl.cfg</code></p>
<blockquote>
<p>confogl_addcvar confogl_pills_limit 4</p>
</blockquote>
<h2 id="修改止疼药加血量"><a class="markdownIt-Anchor" href="#修改止疼药加血量"></a> 修改止疼药加血量</h2>
<p>路径：<code>./cfg/server.cfg</code></p>
<blockquote>
<p>sm_cvar pain_pills_health_value 10 止疼药额外+10(也就是+60) 会影响战役</p>
</blockquote>
<h2 id="让ai生还者吃药加血"><a class="markdownIt-Anchor" href="#让ai生还者吃药加血"></a> 让AI生还者吃药加血</h2>
<p>路径：<code>./addons/sourcemod/plugins/optional/</code></p>
<blockquote>
<p>删除(改名就行)插件botpopstop.smx</p>
</blockquote>
<h2 id="修改地图信息"><a class="markdownIt-Anchor" href="#修改地图信息"></a> 修改地图信息</h2>
<p>路径：<code>./cfg/stripper/zonemod/maps</code></p>
<p>想用原地图把地图配置文件从目录中移除</p>
<h1 id="一些错误的解决"><a class="markdownIt-Anchor" href="#一些错误的解决"></a> 一些错误的解决</h1>
<h2 id="错误1"><a class="markdownIt-Anchor" href="#错误1"></a> 错误1</h2>
<p><strong>-bash: /etc/init.d/srcds1: /bin/sh^M: bad interpreter: No such file or directory</strong></p>
<p>文件编码有问题。</p>
<p>sir的说明:</p>
<blockquote>
<p>If you receive “-bash: /etc/init.d/srcds1: /bin/sh^M: bad interpreter: No such file or directory” error, it means you have dos line ending file You can use dos2unix command on srcds1 file, or use any other method to have this file in unix format</p>
</blockquote>
<p>解决方法1:</p>
<blockquote>
<p>vi打开文件， 输入:set ff=unix， 然后:wq退出</p>
</blockquote>
<p>解决方法2:</p>
<blockquote>
<p>sed -i -e ‘s/\r$//’ 文件名</p>
</blockquote>
<h1 id="其他"><a class="markdownIt-Anchor" href="#其他"></a> 其他</h1>
<h2 id="关于游戏更新"><a class="markdownIt-Anchor" href="#关于游戏更新"></a> 关于游戏更新</h2>
<blockquote>
<p>./steamcmd.sh<br />
login anonymous<br />
app_update 222860 validate</p>
</blockquote>
<p>如果开了插件服务器，则需要等作者更新发布之后，将作者的更新同步到你的服务器</p>
<h2 id="关于screen托盘"><a class="markdownIt-Anchor" href="#关于screen托盘"></a> 关于screen托盘</h2>
<p>查看被托盘的进程，只能看到本账户开的，不能看到其他账户开的。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">screen -ls</span><br><span class="line">screen -r</span><br><span class="line">当只有一个进程时, screen -r会直接进入进程, screen -ls 不会</span><br></pre></td></tr></table></figure>
<p>打开进程</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">screen -r 进程名称或进程号(pid)</span><br><span class="line">当只有一个进程被托盘时, 输入screen -r直接打开该进程</span><br></pre></td></tr></table></figure>
<p>退出进程</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Ctrl + C 直接杀掉进程</span><br><span class="line">Ctrl + A + D 退出进程页面, 进程被托盘</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/06/11/%E6%B1%82%E7%94%9F%E4%B9%8B%E8%B7%AF2zonemod%E8%8D%AF%E6%8A%97%E6%8F%92%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BA/" data-id="cl4a25o7z0000coj38ohr0674" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/linux/" rel="tag">linux</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%9C%8D%E5%8A%A1%E5%99%A8/" rel="tag">服务器</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%B1%82%E7%94%9F%E4%B9%8B%E8%B7%AF2/" rel="tag">求生之路2</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%B8%B8%E6%88%8F/" rel="tag">游戏</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-关于-socket-io实现websocket-部分功能的使用" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/06/11/%E5%85%B3%E4%BA%8E-socket-io%E5%AE%9E%E7%8E%B0websocket-%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" class="article-date">
  <time class="post-time" datetime="2022-06-11T02:29:30.000Z" itemprop="datePublished">
    <span class="post-month">6月</span><br/>
    <span class="post-day">11</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/06/11/%E5%85%B3%E4%BA%8E-socket-io%E5%AE%9E%E7%8E%B0websocket-%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/">关于[socket.io实现websocket]部分功能的使用</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">基础知识</a>,<a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/socket/">socket</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="javascript客户端"><a class="markdownIt-Anchor" href="#javascript客户端"></a> JavaScript客户端</h2>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 连接请求</span></span><br><span class="line">socket = io.<span class="title function_">connect</span>(<span class="string">&#x27;ws://&#x27;</span> + <span class="variable language_">document</span>.<span class="property">domain</span> + <span class="string">&#x27;:&#x27;</span> + location.<span class="property">port</span> + <span class="string">&#x27;/wsmine&#x27;</span>, &#123;</span><br><span class="line">            <span class="attr">reconnectionDelayMax</span>: <span class="number">10000</span>,</span><br><span class="line">    		<span class="comment">//附加在请求里的内容</span></span><br><span class="line">            <span class="attr">query</span>: &#123;</span><br><span class="line">                <span class="string">&quot;cookie&quot;</span>: this_cookie</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"><span class="comment">//连接时触发</span></span><br><span class="line"> socket.<span class="title function_">on</span>(<span class="string">&quot;connect&quot;</span>, <span class="function">(<span class="params">rev</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="title function_">alert</span>(<span class="string">&quot;地图生成时间可能较长，请稍等！\n\n扫雷时请不要快速点击，服务器承受不住！&quot;</span>);</span><br><span class="line">        &#125;)</span><br><span class="line"><span class="comment">//断开时触发</span></span><br><span class="line">socket.<span class="title function_">on</span>(<span class="string">&quot;connect&quot;</span>, <span class="function">(<span class="params">rev</span>) =&gt;</span> &#123;&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//收到特定消息</span></span><br><span class="line">socket.<span class="title function_">on</span>(<span class="string">&quot;your massage name&quot;</span>, <span class="function">(<span class="params">rev</span>) =&gt;</span> &#123;&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="python-flask_socketio-服务端"><a class="markdownIt-Anchor" href="#python-flask_socketio-服务端"></a> python flask_socketio 服务端</h2>
<p>启动脚本</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 文件1</span></span><br><span class="line"><span class="keyword">from</span> flask_socketio <span class="keyword">import</span> SocketIO</span><br><span class="line">clearmind_socketio = SocketIO()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 文件2</span></span><br><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Blueprint</span><br><span class="line">clearmind_blueprint = Blueprint(<span class="string">&#x27;main&#x27;</span>, __name__)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动脚本</span></span><br><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"><span class="keyword">from</span> BackEnd.objects <span class="keyword">import</span> clearmind_socketio</span><br><span class="line"><span class="keyword">from</span> BackEnd.routes <span class="keyword">import</span> clearmind_blueprint</span><br><span class="line"><span class="keyword">from</span> flask_cors <span class="keyword">import</span> CORS</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">create_app</span>(<span class="params">debug = <span class="literal">True</span></span>):</span><br><span class="line">    app = Flask(__name__)</span><br><span class="line">    app.debug = debug</span><br><span class="line">    app.config[<span class="string">&#x27;SECRET_KEY&#x27;</span>] = <span class="string">&#x27;clearmind&#x27;</span></span><br><span class="line">    app.register_blueprint(clearmind_blueprint)</span><br><span class="line">    clearmind_socketio.init_app(app)</span><br><span class="line">    CORS(app, supports_credentials=<span class="literal">True</span>)</span><br><span class="line">    <span class="keyword">from</span> BackEnd <span class="keyword">import</span> events</span><br><span class="line">    <span class="keyword">return</span> app</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    app = create_app()</span><br><span class="line">    clearmind_socketio.run(app, host=<span class="string">&#x27;0.0.0.0&#x27;</span>, port=<span class="number">26666</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>消息规则</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> session, request</span><br><span class="line"><span class="keyword">from</span> flask_socketio <span class="keyword">import</span> emit, join_room, leave_room, disconnect</span><br><span class="line"></span><br><span class="line"><span class="comment"># 命名空间wslogin 连接时响应</span></span><br><span class="line"><span class="meta">@clearmind_socketio.on(<span class="params"><span class="string">&#x27;connect&#x27;</span>, namespace=<span class="string">&#x27;/wslogin&#x27;</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">login_connect</span>():</span><br><span class="line">    print_and_log(<span class="string">&#x27;收到登录请求...&#x27;</span>)</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        username = request.args[<span class="string">&#x27;username&#x27;</span>]</span><br><span class="line">        password = request.args[<span class="string">&#x27;password&#x27;</span>]</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;......&#x27;&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">if</span> <span class="number">4</span> &lt; <span class="number">5</span>:</span><br><span class="line">            <span class="comment"># 发送reply消息</span></span><br><span class="line">        	emit(<span class="string">&#x27;reply&#x27;</span>, reply)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="comment"># 发送reply消息</span></span><br><span class="line">        	emit(<span class="string">&#x27;reply&#x27;</span>, <span class="string">&#x27;deny&#x27;</span>)</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        print_and_log(<span class="built_in">str</span>(<span class="built_in">type</span>(e)),<span class="built_in">str</span>(e))</span><br><span class="line">        disconnect()</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">   </span><br><span class="line"><span class="comment"># 命名空间wslogin 连接断开时响应</span></span><br><span class="line"><span class="meta">@clearmind_socketio.on(<span class="params"><span class="string">&#x27;disconnect&#x27;</span>, namespace=<span class="string">&#x27;/wslogin&#x27;</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">login_disconnect</span>():</span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;登录连接断开时执行&#x27;&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">try</span> : </span><br><span class="line">        username = request.args[<span class="string">&#x27;username&#x27;</span>]</span><br><span class="line">        password = request.args[<span class="string">&#x27;password&#x27;</span>]</span><br><span class="line">    <span class="keyword">except</span>:</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line">    print_and_log(<span class="string">f&#x27;登录连接断开...   <span class="subst">&#123;username&#125;</span>&#x27;</span>)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="comment"># 命名空间wsmine 收到click信息时响应</span></span><br><span class="line"><span class="meta">@clearmind_socketio.on(<span class="params"><span class="string">&#x27;click&#x27;</span>, namespace=<span class="string">&#x27;/wsmine&#x27;</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">mine_click</span>(<span class="params">info</span>):</span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;收到点击地图时间, 把数据中的参数抛给后端处理&#x27;&#x27;&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span> :</span><br><span class="line">        cookie = request.args[<span class="string">&#x27;cookie&#x27;</span>]</span><br><span class="line">        data = json.loads(info)</span><br><span class="line">        x, y = data[<span class="string">&#x27;x&#x27;</span>], data[<span class="string">&#x27;y&#x27;</span>]</span><br><span class="line">        username, tm = cookie_user_dict[cookie]</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        print_and_log(<span class="string">&#x27;&gt;&gt;&gt; error &#x27;</span> +  <span class="built_in">str</span>(<span class="built_in">type</span>(e)) + <span class="string">&#x27; &#x27;</span> + <span class="built_in">str</span>(e))</span><br><span class="line">        disconnect()</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;代码&#x27;&#x27;&#x27;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 广播broadcast消息</span></span><br><span class="line">    emit(<span class="string">&#x27;broadcast&#x27;</span>, json.dumps(&#123;<span class="string">&#x27;x&#x27;</span> : x, <span class="string">&#x27;y&#x27;</span> : y, <span class="string">&#x27;color&#x27;</span> : color, <span class="string">&#x27;timmer&#x27;</span> : timmer, <span class="string">&#x27;username&#x27;</span> : username&#125;), broadcast = <span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;代码&#x27;&#x27;&#x27;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 广播game end消息</span></span><br><span class="line">    emit(<span class="string">&#x27;game end&#x27;</span>, json.dumps(CM_server.rank()), broadcast = <span class="literal">True</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;代码&#x27;&#x27;&#x27;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 广播args 消息</span></span><br><span class="line">    emit(<span class="string">&#x27;args&#x27;</span>, json.dumps(CM_server.args()), broadcast = <span class="literal">True</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/06/11/%E5%85%B3%E4%BA%8E-socket-io%E5%AE%9E%E7%8E%B0websocket-%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" data-id="cl49a2qlk000098j3a8tx2wsx" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/javascript/" rel="tag">javascript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/socket/" rel="tag">socket</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/socket-io/" rel="tag">socket.io</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/websocket/" rel="tag">websocket</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-关于socket部分功能的使用" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/06/11/%E5%85%B3%E4%BA%8Esocket%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" class="article-date">
  <time class="post-time" datetime="2022-06-11T02:00:03.000Z" itemprop="datePublished">
    <span class="post-month">6月</span><br/>
    <span class="post-day">11</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/06/11/%E5%85%B3%E4%BA%8Esocket%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/">关于socket部分功能的使用</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">基础知识</a>,<a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/socket/">socket</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="前言"><a class="markdownIt-Anchor" href="#前言"></a> 前言</h2>
<p>都是复制的以前写过的东西</p>
<h2 id="python-udp-客户端服务端"><a class="markdownIt-Anchor" href="#python-udp-客户端服务端"></a> python UDP 客户端+服务端</h2>
<p>封装进了一个类</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"><span class="keyword">from</span> typing <span class="keyword">import</span> <span class="type">Tuple</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">SR</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, ip = <span class="string">&#x27;0.0.0.0&#x27;</span>, port = <span class="number">27013</span>, sz = <span class="number">2048</span></span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</span><br><span class="line">        self.__socket.bind((ip, port))</span><br><span class="line">        self.__sz = sz</span><br><span class="line">        self.__lock = threading.Lock()</span><br><span class="line">        self.__datapool = []</span><br><span class="line">        </span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__t_receive</span>(<span class="params">self</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;接收消息, 应该放到单独的线程中&#x27;&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">                data, address = self.__socket.recvfrom(self.__sz)</span><br><span class="line">                self.__lock.acquire()</span><br><span class="line">                data = data.decode(<span class="string">&#x27;UTF-8&#x27;</span>)</span><br><span class="line">                self.__datapool.append((data, address))</span><br><span class="line">                self.__lock.release()</span><br><span class="line">            <span class="keyword">except</span>:</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">receive</span>(<span class="params">self</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;开启消息接收的方法&#x27;&#x27;&#x27;</span></span><br><span class="line">        t = threading.Thread(target = self.__t_receive, name = <span class="string">&#x27;receiver&#x27;</span>)</span><br><span class="line">        t.start()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">send</span>(<span class="params">self, data, address</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;发送消息&#x27;&#x27;&#x27;</span></span><br><span class="line">        self.__socket.sendto(data.encode(<span class="string">&#x27;UTF-8&#x27;</span>), address)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">getData</span>(<span class="params">self</span>) -&gt; <span class="type">Tuple</span>[<span class="built_in">str</span>, <span class="type">Tuple</span>[<span class="built_in">str</span>, <span class="built_in">int</span>]]:</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">        从消息队列中提取一条最早的数据</span></span><br><span class="line"><span class="string">        return : (msg, (ip, port))</span></span><br><span class="line"><span class="string">        &#x27;&#x27;&#x27;</span></span><br><span class="line">        <span class="comment">#self.__lock.acquire()</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">len</span>(self.__datapool) == <span class="number">0</span>: <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line">        data = self.__datapool.pop(<span class="number">0</span>)</span><br><span class="line">        <span class="comment">#self.__lock.release()</span></span><br><span class="line">        <span class="keyword">return</span> data</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">inactive</span>(<span class="params">self</span>):</span><br><span class="line">        self.__socket.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="python-tcp-服务端"><a class="markdownIt-Anchor" href="#python-tcp-服务端"></a> python TCP 服务端</h2>
<p>一个摆烂的服务端脚本删减</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># from MemoSqliteFunctions import *</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"></span><br><span class="line">HOST = <span class="string">&#x27;0.0.0.0&#x27;</span></span><br><span class="line">PORT = <span class="number">22222</span></span><br><span class="line">BUFF_SIZE = <span class="number">4096</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">service_run</span>():</span><br><span class="line">    <span class="comment"># 建立Socket连接, AF_INEF说明使用IPv4地址, SOCK_STREAM指明TCP协议</span></span><br><span class="line">    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">    serverSocket.bind((HOST, PORT))</span><br><span class="line">    serverSocket.listen(<span class="number">3</span>)<span class="comment"># 监听 </span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;Run at <span class="subst">&#123;HOST&#125;</span>:<span class="subst">&#123;PORT&#125;</span>&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 接收TCP连接, 并返回新的Socket对象</span></span><br><span class="line">        sk, addr = serverSocket.accept()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;客户端: <span class="subst">&#123;addr&#125;</span> 链接&quot;</span>)</span><br><span class="line"></span><br><span class="line">        task = threading.Thread(target = TCP_task, args=(sk, ), name = <span class="string">&#x27;tcp_task&#x27;</span>)</span><br><span class="line">        task.start()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        conn.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">TCP_task</span>(<span class="params">sk : socket.socket</span>) -&gt; <span class="literal">None</span>:</span><br><span class="line">	<span class="string">&#x27;&#x27;&#x27;线程任务&#x27;&#x27;&#x27;</span></span><br><span class="line">	</span><br><span class="line">    <span class="comment"># ......</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            <span class="comment"># 接收客户端发送的数据</span></span><br><span class="line">            data = sk.recv(BUFF_SIZE)</span><br><span class="line">            data = data.decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">            <span class="comment"># ......</span></span><br><span class="line">		<span class="keyword">except</span>:</span><br><span class="line">            <span class="comment"># ......</span></span><br><span class="line">            <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    service_run()</span><br></pre></td></tr></table></figure>
<h2 id="javaandroid开发-udp-客户端"><a class="markdownIt-Anchor" href="#javaandroid开发-udp-客户端"></a> java(Android开发) UDP 客户端</h2>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">uploadUDP</span><span class="params">(String ip, String port)</span>&#123;</span><br><span class="line">    <span class="keyword">new</span> <span class="title class_">Thread</span>() &#123;</span><br><span class="line">        <span class="comment">//线程运行的代码</span></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="comment">// Toast弹窗必要条件1</span></span><br><span class="line">                Looper.prepare();</span><br><span class="line">                </span><br><span class="line">                <span class="type">InetAddress</span> <span class="variable">address</span> <span class="operator">=</span> InetAddress.getByName(ip);</span><br><span class="line">                <span class="type">DatagramSocket</span> <span class="variable">socket</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DatagramSocket</span>();</span><br><span class="line"></span><br><span class="line">                <span class="type">String</span> <span class="variable">data</span> <span class="operator">=</span> <span class="string">&quot;xxxxxxx&quot;</span>;	</span><br><span class="line">		</span><br><span class="line">                <span class="comment">// 发消息</span></span><br><span class="line">                <span class="type">byte</span>[] data1 = data.getBytes();</span><br><span class="line">                <span class="type">DatagramPacket</span> <span class="variable">packet</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DatagramPacket</span>(data1, data1.length, address, Integer.parseInt(port));</span><br><span class="line">                socket.send(packet);</span><br><span class="line"></span><br><span class="line">                <span class="comment">//收消息</span></span><br><span class="line">                <span class="type">byte</span>[] data2 = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1</span> &lt;&lt; <span class="number">5</span>];</span><br><span class="line">                <span class="type">DatagramPacket</span> <span class="variable">packet2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DatagramPacket</span>(data2, data2.length);</span><br><span class="line">                socket.receive(packet2);</span><br><span class="line">                </span><br><span class="line">                <span class="type">String</span> <span class="variable">reply</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(data2, <span class="number">0</span>, packet2.getLength());</span><br><span class="line">                </span><br><span class="line">                Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;receive : &quot;</span>+reply, Toast.LENGTH_SHORT).show();</span><br><span class="line"></span><br><span class="line">                <span class="comment">// Toast弹窗必要条件1</span></span><br><span class="line">                Looper.loop();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;.start();<span class="comment">//启动线程</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="javaandroid开发-tcp-客户端"><a class="markdownIt-Anchor" href="#javaandroid开发-tcp-客户端"></a> java(Android开发) TCP 客户端</h2>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">upload</span><span class="params">(String ip, String port)</span>&#123;</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>() &#123;</span><br><span class="line">            <span class="comment">//线程运行的代码</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                <span class="type">boolean</span> <span class="variable">know_error</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">                </span><br><span class="line">                <span class="comment">// Toast弹窗必要条件1</span></span><br><span class="line">                Looper.prepare();</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">					<span class="comment">// ......</span></span><br><span class="line">                    </span><br><span class="line">                    InetAddress serverip= InetAddress.getByName(ip);;<span class="comment">//定义保存服务器地址的对象</span></span><br><span class="line">                    Socket client=<span class="keyword">new</span> <span class="title class_">Socket</span>(serverip, Integer.parseInt(port));<span class="comment">//定义创建客户端对象的Socket</span></span><br><span class="line">                    </span><br><span class="line">                    OutputStream socketOut=client.getOutputStream(); <span class="comment">//定义发送信息的输出流对象</span></span><br><span class="line">                    InputStream socketIn=client.getInputStream(); <span class="comment">//定义接收数据的输入流</span></span><br><span class="line"></span><br><span class="line">                    <span class="type">byte</span> receive[] = <span class="keyword">new</span> <span class="title class_">byte</span>[buff_size]; <span class="comment">//定义保存客户端发送来的数据的字节数组</span></span><br><span class="line"></span><br><span class="line">                    <span class="comment">// ......</span></span><br><span class="line">                    </span><br><span class="line">                    <span class="comment">//发消息</span></span><br><span class="line">                    <span class="type">String</span> <span class="variable">firstData</span> <span class="operator">=</span> <span class="string">&quot;xxxxxxx&quot;</span>;</span><br><span class="line">                    socketOut.write(firstData.getBytes(<span class="string">&quot;utf-8&quot;</span>));</span><br><span class="line">                    </span><br><span class="line">                    <span class="comment">//接收数据保存在字节数组中, 然后转String字符串</span></span><br><span class="line">                    <span class="type">int</span> len=socketIn.read(receive);</span><br><span class="line">                    String rev=<span class="keyword">new</span> <span class="title class_">String</span>(receive,<span class="number">0</span>,len);</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">if</span> (! rev.equals(<span class="string">&quot;ok&quot;</span>))&#123;</span><br><span class="line">                        know_error = <span class="literal">true</span>;</span><br><span class="line">                        Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;拒绝访问&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Exception</span>(<span class="string">&quot;access deny&quot;</span>);</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">for</span>(<span class="type">int</span> row=<span class="number">0</span>; row&lt;rowSize ; row++) &#123;</span><br><span class="line"></span><br><span class="line">                        <span class="type">String</span> <span class="variable">data</span> <span class="operator">=</span> Integer.toString(row);</span><br><span class="line"></span><br><span class="line">                        <span class="comment">// 发消息</span></span><br><span class="line">                        socketOut.write(data.getBytes(<span class="string">&quot;utf-8&quot;</span>));</span><br><span class="line"></span><br><span class="line">                        <span class="comment">//接收数据保存在字节数组中, 然后转String字符串</span></span><br><span class="line">                        len=socketIn.read(receive);</span><br><span class="line">                        rev=<span class="keyword">new</span> <span class="title class_">String</span>(receive,<span class="number">0</span>,len);</span><br><span class="line">                        </span><br><span class="line">                        <span class="keyword">if</span> (! rev.equals(<span class="string">&quot;ok&quot;</span>))&#123;</span><br><span class="line">                            know_error = <span class="literal">true</span>;</span><br><span class="line">                            Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;传输中遇到错误&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Exception</span>(<span class="string">&quot;error at half road&quot;</span>);</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">                    &#125;</span><br><span class="line">                    socketOut.close();</span><br><span class="line">                    socketIn.close();</span><br><span class="line">                    client.close();</span><br><span class="line"></span><br><span class="line">                    Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;同步完成&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                &#125;<span class="keyword">catch</span>(ConnectException e)&#123;</span><br><span class="line">                    Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;未能成功连接服务器&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">catch</span>(Exception e)&#123;</span><br><span class="line">                    Log.i(<span class="string">&quot;tcp-error&quot;</span>, e.getMessage() + e.getStackTrace() + e.getClass());</span><br><span class="line">                    <span class="keyword">if</span>(! know_error)&#123;</span><br><span class="line">                        Toast.makeText(CloudActivity.<span class="built_in">this</span>, <span class="string">&quot;发生错误&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                </span><br><span class="line">                <span class="comment">// Toast弹窗必要条件2</span></span><br><span class="line">                Looper.loop();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;.start();<span class="comment">//启动线程</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/06/11/%E5%85%B3%E4%BA%8Esocket%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD%E7%9A%84%E4%BD%BF%E7%94%A8/" data-id="cl49a2qlp000198j3hxz4dzgi" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/" rel="tag">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/socket/" rel="tag">socket</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-Git使用总结-仅部分功能" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/06/09/Git%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93-%E4%BB%85%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD/" class="article-date">
  <time class="post-time" datetime="2022-06-09T14:01:28.000Z" itemprop="datePublished">
    <span class="post-month">6月</span><br/>
    <span class="post-day">09</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/06/09/Git%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93-%E4%BB%85%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD/">Git使用总结(仅部分功能)</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">基础知识</a>,<a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/git/">git</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="部分全局配置"><a class="markdownIt-Anchor" href="#部分全局配置"></a> 部分全局配置</h2>
<p>修改全局配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --global 配置名称 配置新值</span><br></pre></td></tr></table></figure>
<p>部分全局配置</p>
<p>用户名</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config user.name</span><br></pre></td></tr></table></figure>
<p>邮箱</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config user.email</span><br></pre></td></tr></table></figure>
<p>HTTP和HTTPS代理</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config http.proxy</span><br><span class="line">git config https.proxy</span><br></pre></td></tr></table></figure>
<h2 id="新建一个库时github给出的初始化代码"><a class="markdownIt-Anchor" href="#新建一个库时github给出的初始化代码"></a> 新建一个库时Github给出的初始化代码</h2>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;# New Repo&quot; &gt;&gt; README.md</span><br><span class="line">git init</span><br><span class="line">git add README.md</span><br><span class="line">git commit -m &quot;first commit&quot;</span><br><span class="line">git branch -M main</span><br><span class="line">git remote add origin https://github.com/Wldcmzy/234.git</span><br><span class="line">git push -u origin main</span><br></pre></td></tr></table></figure>
<h4 id="解析"><a class="markdownIt-Anchor" href="#解析"></a> 解析</h4>
<p>初始化</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git init</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="http://xn--READM-gq2j412n.md">建立READM.md</a> 文件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;# New Repo&quot; &gt;&gt; README.md</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="http://xn--README-2e4jg12p.md">添加README.md</a> 至工作缓存</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git add README.md</span><br></pre></td></tr></table></figure>
<p>将缓存提交至本地库</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -m &quot;first commit&quot;</span><br></pre></td></tr></table></figure>
<p>更改当前分支名字为main</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -M main</span><br></pre></td></tr></table></figure>
<p>设置远程连接的仓库</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add origin https://github.com/Wldcmzy/234.git</span><br></pre></td></tr></table></figure>
<p>将本地仓库提交至远程仓库（将本地origin提交至远程main分支, 写-u设默认，以后只需要git push）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -u origin main</span><br></pre></td></tr></table></figure>
<h4 id="可以不修改分支名字"><a class="markdownIt-Anchor" href="#可以不修改分支名字"></a> 可以不修改分支名字</h4>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git init</span><br><span class="line">git add README.md</span><br><span class="line">git commit -m &quot;first commit&quot;</span><br><span class="line">git remote add origin https://github.com/Wldcmzy/234.git</span><br><span class="line">git push -u origin master</span><br></pre></td></tr></table></figure>
<h2 id="从远程获取最新数据"><a class="markdownIt-Anchor" href="#从远程获取最新数据"></a> 从远程获取最新数据</h2>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull origin master</span><br></pre></td></tr></table></figure>
<h2 id="其他分支的切换和提交"><a class="markdownIt-Anchor" href="#其他分支的切换和提交"></a> 其他分支的切换和提交</h2>
<h4 id="切换分支"><a class="markdownIt-Anchor" href="#切换分支"></a> 切换分支</h4>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout 分支名称</span><br></pre></td></tr></table></figure>
<h4 id="新建分支"><a class="markdownIt-Anchor" href="#新建分支"></a> 新建分支</h4>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b 分支名称</span><br></pre></td></tr></table></figure>
<h4 id="新建空的分支"><a class="markdownIt-Anchor" href="#新建空的分支"></a> 新建空的分支</h4>
<p>使用<code>git checkout -b</code>命令创建的分支是有父节点。</p>
<p>使用<code>git checkout --orphan</code>命令，创建孤立分支</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout --orphan</span><br></pre></td></tr></table></figure>
<p>使用<code>git rm -rf .</code>来清除拷贝的内容</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rm -rf .</span><br></pre></td></tr></table></figure>
<p>现在获得了一个空的文件夹，但是现在分支没完全有，使用<code>git branch -a</code>也是看不到新分支的。</p>
<p>再随便加一点东西（<a target="_blank" rel="noopener" href="http://xn--README-hh4kj42j.md">比如README.md</a>），提交上去。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git add .</span><br><span class="line">git commit -m &#x27;new branch&#x27;</span><br></pre></td></tr></table></figure>
<p>然后分支就有了，可以把它提交到远程仓库。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin 新分支</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/06/09/Git%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93-%E4%BB%85%E9%83%A8%E5%88%86%E5%8A%9F%E8%83%BD/" data-id="cl47430f300005sj3fruifdqy" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/git/" rel="tag">git</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-对多人扫雷课设核心算法的分析" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/06/06/%E5%AF%B9%E5%A4%9A%E4%BA%BA%E6%89%AB%E9%9B%B7%E8%AF%BE%E8%AE%BE%E6%A0%B8%E5%BF%83%E7%AE%97%E6%B3%95%E7%9A%84%E5%88%86%E6%9E%90/" class="article-date">
  <time class="post-time" datetime="2022-06-06T13:20:39.000Z" itemprop="datePublished">
    <span class="post-month">6月</span><br/>
    <span class="post-day">06</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/06/06/%E5%AF%B9%E5%A4%9A%E4%BA%BA%E6%89%AB%E9%9B%B7%E8%AF%BE%E8%AE%BE%E6%A0%B8%E5%BF%83%E7%AE%97%E6%B3%95%E7%9A%84%E5%88%86%E6%9E%90/">对多人扫雷课设核心算法的分析</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1/">课程设计</a>,<a class="article-category-link" href="/categories/%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1/%E7%BD%91%E9%A1%B5%E7%89%88%E5%A4%9A%E4%BA%BA%E6%89%AB%E9%9B%B7/">网页版多人扫雷</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="序言"><a class="markdownIt-Anchor" href="#序言"></a> 序言</h1>
<p>一开始打算自己写的，但想起来无敌的xxy早在报告中介绍过这一部分了，便直接拿过来用乐。</p>
<h1 id="项目连接"><a class="markdownIt-Anchor" href="#项目连接"></a> 项目连接</h1>
<p><a target="_blank" rel="noopener" href="https://github.com/Wldcmzy/Multiplayer-MineSweeper">Wldcmzy/Multiplayer-MineSweeper: 网页版多人扫雷 web前端+python flask后端+websocket通讯 JS/CSS代码内置于HTML (github.com)</a></p>
<h1 id="一些定义"><a class="markdownIt-Anchor" href="#一些定义"></a> 一些定义</h1>
<p><strong>复制时序号错乱，不以序号为准。</strong></p>
<p>（7）开荒：用户点击一个未知状态的方格后，根据开荒规则打开一片区域。开荒有2个阶段：点击开荒、遍历开荒，分别对应两种开荒规则，规则请参照定义第8、9条。</p>
<p>（8）点击开荒规则：点击开荒阶段的规则。先打开用户点击位置的方格。如果用户点击位置方格打开状态不是雷，则打开八方位方格中打开状态为0的方格。</p>
<p>（9）遍历开荒规则：遍历开荒阶段的规则。对打开的八方位方格中的方格继续进行开荒。如果方格打开状态是0，打开八方位的全部方格，然后重复执行该遍历开荒规则；如果方格打开状态是1、2、3、4、5、6、7、8，停止当前方格的遍历开荒。</p>
<p>（1） 时间戳：用户点击请求到达服务器的相对时间顺序。</p>
<p>（2） 用户颜色：每个用户在每一局游戏中会获得一个颜色，雷区地图上用用户颜色标识用户开荒区域。同一用户在同一局游戏中始终具有相同的用户颜色，用户的退出不能更改本局的用户颜色。游戏重开后，用户颜色重置。</p>
<p>（3） 染色：将用户开荒区域的方格由初始颜色变更为用户颜色。</p>
<p>（4） 染色修正：用户颜色的修正。在多人进行游戏时，因为数据传输的延迟，所以前端收到的广播顺序不一定是操作到达服务器的顺序。所以有可能出现用户A的点击请求先到达服务端，由于服务端还未广播A的点击或者前端收到了广播还没有进行开荒，用户B可能点击了和用户A开荒的区域有交集的位置，用户B的点击请求也会到达服务端。服务器会先广播A的点击，后广播B的点击。用户B的广播可能先到前端，这样前端就会将区域判定为用户B的区域导致错误。所以增加染色修正的概念。前端会根据染色修正规则将开荒区域调整为正确的颜色。染色修正规则请参照定义第18条。</p>
<p>（5） 染色修正规则：以时间戳为依据，方格的颜色为时间戳最小的用户对应的用户颜色。</p>
<h1 id="算法分析"><a class="markdownIt-Anchor" href="#算法分析"></a> 算法分析</h1>
<h2 id="1-雷区生成算法"><a class="markdownIt-Anchor" href="#1-雷区生成算法"></a> 1、雷区生成算法</h2>
<p>雷区生成算法的参数有：子区域行列数、子区域雷数、雷区地图行数、雷区地图列数、模数、雷区种子、斐波那契数列值。</p>
<p>设置子区域的目的是为了防止部分区域雷的密度不够，一次开荒出大片区域，递归栈空间不够用。同时也是为了更好的用户体验。将雷区地图按照子区域行列数进行划分，依次进行每个子区域的地图生成。划分边界的不够完整的子区域的雷区，按照完整子区域的雷的密度计算得出该区域的雷数。</p>
<p>斐波那契数列值用于辅助进行子区域布雷，每局雷区地图生成初始为数列第1项。</p>
<p>子区域地图生成：</p>
<p>（1）子区域按顺序从上到下、从左到右对每个方格依次进行编号，编号从0开始，每次递增1。最终编号小于子区域雷数的方格为有雷的方格。</p>
<p>（2）定义辅助变量delta，初始化令其等于雷区种子</p>
<p>（3）子区域按顺序从上到下、从左到右枚举，对枚举到的每个格子进行编号的交换。交换规则：先递推一次斐波那契数列，辅助变量delta和斐波那契数列值相乘，并对模数取模。若取模结果大于子区域雷数，持续和斐波那契数列值相乘，直至取模结果小于子区域雷数。delta%行数为交换位置的子区域行号。再将delta除以子区域行数，delta%列数为交换位置的子区域列号。交换枚举位置和交换位置的两个方格的数字。</p>
<p>（4）将子区域地图信息转移到雷区地图上。</p>
<h2 id="2-开荒算法"><a class="markdownIt-Anchor" href="#2-开荒算法"></a> 2、开荒算法</h2>
<p>根据定义中的点击开荒规则和遍历开荒规则进行dfs遍历雷区地图。具体有四种情况：</p>
<p>（1）前端同步本局历史数据时：由于接收到的历史数据是按按照时间戳排好序的，所以直接按照规则进行dfs遍历，同时在遍历过程中进行染色。</p>
<p>（2）前端收到广播时：由于收到的广播顺序不一定是按时间戳排好序的，所以按照规则进行dfs遍历，同时在遍历过程中进行染色，如果在dfs遍历中发生要对已染色的区域再次染色，则参考染色修正规则。</p>
<p>（3）前端进行用户本地开荒时：只按照规则进行dfs遍历，不染色。</p>
<p>（4）后端遍历：因为时间戳表示到达后端的时间，所以后端直接判断初始位置是否被遍历过，若没有，进行遍历即可。</p>
<h2 id="3-用户颜色分配算法"><a class="markdownIt-Anchor" href="#3-用户颜色分配算法"></a> 3、用户颜色分配算法</h2>
<p>颜色库中规定多种色调的颜色，每种色调有若干亮度不同的颜色。</p>
<p>（1）确定色调：随机一个颜色范围不大于色调的随机数，若该色调使用次数为色调最少使用次数，则确定为该色调，否则向后循环遍历色调，直到找到使用次数为色调最少使用次数的色调。</p>
<p>（2）确定颜色：随机一个不大于该色调颜色数的随机数，若该颜色使用次数为色调内颜色最少使用次数，则确定为该颜色，否则向后循环遍历色调内的颜色，直到找到使用次数为色调内颜色最少使用次数的色调。</p>
<h2 id="4-染色修正规则"><a class="markdownIt-Anchor" href="#4-染色修正规则"></a> 4、染色修正规则</h2>
<p>在染色的时候，永远遵循染时间戳更小的用户颜色。即在发生要对已染色的区域再次染色时：</p>
<p>（1）如果原方格的时间戳更小，则颜色不更改。</p>
<p>（2）如果新的时间戳更小，则更改方格的颜色和时间戳。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/06/06/%E5%AF%B9%E5%A4%9A%E4%BA%BA%E6%89%AB%E9%9B%B7%E8%AF%BE%E8%AE%BE%E6%A0%B8%E5%BF%83%E7%AE%97%E6%B3%95%E7%9A%84%E5%88%86%E6%9E%90/" data-id="cl42s4wxo0000bwj3g9mufu22" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%89%AB%E9%9B%B7/" rel="tag">扫雷</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E7%AE%97%E6%B3%95/" rel="tag">算法</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-linux服务器上通过python脚本自动控制mysql数据库" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/06/06/linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%80%9A%E8%BF%87python%E8%84%9A%E6%9C%AC%E8%87%AA%E5%8A%A8%E6%8E%A7%E5%88%B6mysql%E6%95%B0%E6%8D%AE%E5%BA%93/" class="article-date">
  <time class="post-time" datetime="2022-06-06T12:55:40.000Z" itemprop="datePublished">
    <span class="post-month">6月</span><br/>
    <span class="post-day">06</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/06/06/linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%80%9A%E8%BF%87python%E8%84%9A%E6%9C%AC%E8%87%AA%E5%8A%A8%E6%8E%A7%E5%88%B6mysql%E6%95%B0%E6%8D%AE%E5%BA%93/">python语言实现服务器对mysql数据库的自动处理</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a>,<a class="article-category-link" href="/categories/%E5%90%8E%E7%AB%AF/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="声明"><a class="markdownIt-Anchor" href="#声明"></a> 声明</h2>
<p>1.本人还是比较菜的，对服务器安全性等内容可能一窍不通，也没有使用框架，仅考虑完成目的。</p>
<p>2.本篇博客仅记录使用本人的总结，仅论述实现目的的基本流程， 不详细讲解知识，数据库初学者建议先学习sql基本语法(时间成本不高)</p>
<h2 id="环境安装"><a class="markdownIt-Anchor" href="#环境安装"></a> 环境安装</h2>
<p>我使用的操作系统是Ubuntu20, 装好之后是自带python3, 所以我们直接无脑安装mysql</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt update</span><br><span class="line">apt install mysql-server</span><br></pre></td></tr></table></figure>
<p>非root账号输入, 当然要能获取root权限才行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt update</span><br><span class="line">sudo apt install mysql-server</span><br></pre></td></tr></table></figure>
<h2 id="新建一个账号"><a class="markdownIt-Anchor" href="#新建一个账号"></a> 新建一个账号</h2>
<p>这一步没什么用，只是我想新建一个</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">adduser --home /usr/sir sir</span><br></pre></td></tr></table></figure>
<h2 id="mysql登录"><a class="markdownIt-Anchor" href="#mysql登录"></a> mysql登录</h2>
<p>mysql数据库创建时会自动生成一串root用户密码,想改密码的可以去查查怎么改。使用root账号可以直接这样登录</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql</span><br></pre></td></tr></table></figure>
<p>非root账户用用户名密码登录, 输入下方语句后会提示你输入密码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -u username -p</span><br></pre></td></tr></table></figure>
<h2 id="数据库基本操作"><a class="markdownIt-Anchor" href="#数据库基本操作"></a> 数据库基本操作</h2>
<p>建增删改查等操作示例。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">select ifUsed from invitation where invitationCode = &#x27;xxx&#x27;;</span><br><span class="line">update invitation set ifUsed = &#x27;xxx&#x27; where invitationCode = &#x27;xxx&#x27;;</span><br><span class="line">delete from invitation where username = &#x27;xxx&#x27;;</span><br><span class="line">insert into userInfo values (&#x27;xxx&#x27;, &#x27;xxx&#x27;, &#x27;xxx&#x27;, 0, 0, 0);</span><br></pre></td></tr></table></figure>
<p>创建一个数据库用户, @后跟一个ip, 表示允许哪些ip,或哪些子网段的ip访问, 若写%表示所有, localhost顾名思义是本地</p>
<p>identified by 后面写密码</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> <span class="string">&#x27;testdbuser&#x27;</span>@<span class="string">&#x27;localhost&#x27;</span> identified <span class="keyword">by</span> <span class="string">&#x27;abcd&#x27;</span>;</span><br></pre></td></tr></table></figure>
<p>给用户授权， 当然也可以新建角色给角色授权，然后再把角色权限授权给用户，</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> privileges <span class="keyword">on</span> <span class="keyword">table</span> 好几个表 <span class="keyword">to</span> testdbuser<span class="variable">@localhost</span></span><br></pre></td></tr></table></figure>
<h2 id="python自动操作数据库"><a class="markdownIt-Anchor" href="#python自动操作数据库"></a> python自动操作数据库</h2>
<h4 id="安装pymysql库"><a class="markdownIt-Anchor" href="#安装pymysql库"></a> 安装pymysql库</h4>
<p>无脑安装</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install mypysql</span><br></pre></td></tr></table></figure>
<h4 id="pymysql基本操作"><a class="markdownIt-Anchor" href="#pymysql基本操作"></a> pymysql基本操作</h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pymysql</span><br></pre></td></tr></table></figure>
<h5 id="第一步"><a class="markdownIt-Anchor" href="#第一步"></a> 第一步</h5>
<p>首先创建<strong>对象</strong>和<strong>游标</strong>， 形如下方代码</p>
<p>参数database表示你要操作哪个数据库 ，用户名密码要正确，且用户要有访问权限才行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">self.__connection = pymysql.connect(host = self.__host, user = self.__user, password = self.__password, database = self.__database) <span class="comment"># 实例化对象</span></span><br><span class="line">        </span><br><span class="line">self.__cursor = self.__connection.cursor(cursor=pymysql.cursors.DictCursor) <span class="comment">#游标</span></span><br></pre></td></tr></table></figure>
<h5 id="增删改查"><a class="markdownIt-Anchor" href="#增删改查"></a> 增删改查</h5>
<p>注意除了查询操作外，其他操作(也就是会改变数据库内容的操作)需要最后使用**commit()**方法,否则无法成功真正在数据库中完成操作</p>
<p>查</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sql = <span class="string">&#x27;select count(*) cnt from User where uid = %s&#x27;</span></span><br><span class="line">self.__cursor.execute(sql, (uid)) <span class="comment"># 使用execute执行</span></span><br><span class="line">cnt = self.__cursor.fetchone()[<span class="string">&#x27;cnt&#x27;</span>] <span class="comment">#  使用fetchone() fetchmany() fetchall() 获取返回结果的一条、多条或全部</span></span><br><span class="line"><span class="keyword">if</span> cnt == <span class="number">0</span>: <span class="keyword">return</span> <span class="string">&#x27;无该用户&#x27;</span></span><br></pre></td></tr></table></figure>
<p>增</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sql = <span class="string">&#x27;insert into User values (%s, %s, %s, %s)&#x27;</span></span><br><span class="line">self.__cursor.execute(sql, (cnt + <span class="number">1</span>, name, password, <span class="literal">False</span>))</span><br><span class="line">self.__connection.commit() <span class="comment">#操作设计数据库修改需要执行commit方法</span></span><br></pre></td></tr></table></figure>
<p>其他的都差不多</p>
<p>根据使用经验，若传参为字符串自带引号，当然可以提前写好之后只execute(sql)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sql = <span class="string">&#x27;insert into User values (%s, %s, %s, %s)&#x27;</span> %(乱七八糟)</span><br><span class="line">self.__cursor.execute(sql)</span><br></pre></td></tr></table></figure>
<h5 id="长时间未操作连接断开"><a class="markdownIt-Anchor" href="#长时间未操作连接断开"></a> 长时间未操作连接断开</h5>
<p>Connection对象的ping 方法可以检查是否连接还在，将参数reconnect设为True表示若连接断开自动重连</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">self.__connection.ping(reconnect=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/06/06/linux%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%80%9A%E8%BF%87python%E8%84%9A%E6%9C%AC%E8%87%AA%E5%8A%A8%E6%8E%A7%E5%88%B6mysql%E6%95%B0%E6%8D%AE%E5%BA%93/" data-id="cl2uhoebl000je4j357n08fg9" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/linux/" rel="tag">linux</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/socket/" rel="tag">socket</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/sql/" rel="tag">sql</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%90%8E%E7%AB%AF/" rel="tag">后端</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="tag">数据库</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%9C%8D%E5%8A%A1%E5%99%A8/" rel="tag">服务器</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-爬取自己CSDN上所有的博客并转存为md文件" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/05/06/%E7%88%AC%E5%8F%96%E8%87%AA%E5%B7%B1CSDN%E4%B8%8A%E6%89%80%E6%9C%89%E7%9A%84%E5%8D%9A%E5%AE%A2%E5%B9%B6%E8%BD%AC%E5%AD%98%E4%B8%BAmd%E6%96%87%E4%BB%B6/" class="article-date">
  <time class="post-time" datetime="2022-05-06T09:59:32.000Z" itemprop="datePublished">
    <span class="post-month">5月</span><br/>
    <span class="post-day">06</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/05/06/%E7%88%AC%E5%8F%96%E8%87%AA%E5%B7%B1CSDN%E4%B8%8A%E6%89%80%E6%9C%89%E7%9A%84%E5%8D%9A%E5%AE%A2%E5%B9%B6%E8%BD%AC%E5%AD%98%E4%B8%BAmd%E6%96%87%E4%BB%B6/">爬取自己CSDN上所有的博客并转存为md文件</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/">教练我想学挂边躲牛</a>,<a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/%E7%88%AC%E8%99%AB/">爬虫</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="1先考虑怎么获得自己所有博客的地址"><a class="markdownIt-Anchor" href="#1先考虑怎么获得自己所有博客的地址"></a> 1.先考虑怎么获得自己所有博客的地址</h2>
<p>a.打开自己的个人博客，尝试从HTML里提取有用信息，没能成功。</p>
<p>b.发现下滑后动态加载自己的博客，于是抓包，找到连接</p>
<img src="/2022/05/06/%E7%88%AC%E5%8F%96%E8%87%AA%E5%B7%B1CSDN%E4%B8%8A%E6%89%80%E6%9C%89%E7%9A%84%E5%8D%9A%E5%AE%A2%E5%B9%B6%E8%BD%AC%E5%AD%98%E4%B8%BAmd%E6%96%87%E4%BB%B6/zhuabaolianjie.png" class="" title="zhuabaolianjie">
<p>c.发现连接中除了page=%d这一部分外，其余都相同，所以考虑使用for循环请求多个连接，并在返回数据中提取网址</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"></span><br><span class="line">N = <span class="number">6</span></span><br><span class="line">idname = <span class="string">&#x27;wldcmzy&#x27;</span></span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36 Edg/101.0.1210.32&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">url_base = <span class="string">&#x27;https://blog.csdn.net/community/home-api/v1/get-business-list?page=&#123;page&#125;&amp;size=20&amp;businessType=lately&amp;noMore=false&amp;username=&#123;name&#125;&#x27;</span>.<span class="built_in">format</span>(page = <span class="string">&#x27;&#123;page&#125;&#x27;</span>, name = idname)</span><br><span class="line"></span><br><span class="line">lst = []</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, N):</span><br><span class="line">    res = requests.get(url_base.<span class="built_in">format</span>(page = i), headers = headers)</span><br><span class="line">    lst.extend(re.findall(<span class="string">&quot;https://blog.csdn.net/%s/article/details/[0-9]+&quot;</span> % idname, res.text))</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="2写抓取博文的代码"><a class="markdownIt-Anchor" href="#2写抓取博文的代码"></a> 2.写抓取博文的代码</h2>
<p>由于本人太菜了，没有成功吧博文提取出来，所以去网上搜索到了<a target="_blank" rel="noopener" href="https://blog.csdn.net/haojie_duan/article/details/122864466">一篇大佬的文章</a>，这篇文章是爬取CSDN热门文章的，可以复制源码略加改动，打到爬自己博文的目的</p>
<p>中间还是遇到了一些错误的，毕竟自己的需求和原文有所不同，比如大佬的代码里没有处理特殊字符\t，以及使用BeautifSoup检索标签时需要在class_参数中加上&quot;markdown_views prism-atom-one-dark&quot;。这些问题可以很快被发现并且修正。</p>
<p>附上改后的代码:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">@Author:survive</span></span><br><span class="line"><span class="string">@Blog(个人博客地址): https://blog.csdn.net/haojie_duan</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string">@File:csdn.py.py</span></span><br><span class="line"><span class="string">@Time:2022/2/10 8:49</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string">@Motto:我不知道将去何方，但我已在路上。——宫崎骏《千与千寻》</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">代码思路：</span></span><br><span class="line"><span class="string">1.确定目标需求:将csdn文章内容保存成 html、PDF、md格式</span></span><br><span class="line"><span class="string">    - 1.1首先保存为html格式：获取列表页中所有的文章ur1地址，请求文章ur1地址获取我们需要的文章内容</span></span><br><span class="line"><span class="string">    - 1.2 通过 wkhtmitopdf.exe把html文件转换成PDF文件</span></span><br><span class="line"><span class="string">    - 1.3 通过 wkhtmitopdf.exe把html文件转换成md文件</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.请求ur1获取网页源代码</span></span><br><span class="line"><span class="string">3.解析数据，提取自己想要内容</span></span><br><span class="line"><span class="string">4.保存数据</span></span><br><span class="line"><span class="string">5.转换数据类型把HTML转换成PDF、md文伴</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">html_str = <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="string">&lt;html lang=&quot;en&quot;&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">    &lt;meta charset=&quot;UTF-8&quot;&gt;</span></span><br><span class="line"><span class="string">    &lt;title&gt;Document&lt;/title&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&#123;article&#125;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> parsel</span><br><span class="line"><span class="comment">#import pdfkit   #用来将html转为pdf</span></span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> urllib.parse</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">import</span> html2text    <span class="comment">#用来将html转换为md</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="comment"># user_agent库：每次执行一次访问随机选取一个 user_agent，防止过于频繁访问被禁止</span></span><br><span class="line">USER_AGENT_LIST = [</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24&quot;</span>,</span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24&quot;</span></span><br><span class="line">    <span class="string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36&quot;</span></span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">CSDNSpider</span>():</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, lst</span>):</span><br><span class="line">        self.url = <span class="string">&#x27;https://blog.csdn.net/csdndevelopers/category_10594816.html&#x27;</span></span><br><span class="line">        self.headers = &#123;</span><br><span class="line">            <span class="string">&#x27;user-agent&#x27;</span>:random.choice(USER_AGENT_LIST)</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        self.lst = lst</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">send_request</span>(<span class="params">self, url</span>):</span><br><span class="line">        response = requests.get(url=url, headers=self.headers)</span><br><span class="line">        response.encoding = <span class="string">&quot;utf-8&quot;</span></span><br><span class="line">        <span class="keyword">if</span> response.status_code == <span class="number">200</span>:</span><br><span class="line">            <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">parse_content</span>(<span class="params">self, reponse</span>):</span><br><span class="line">        html = reponse.text</span><br><span class="line">        selector = parsel.Selector(html)</span><br><span class="line">        href = selector.css(<span class="string">&#x27;.column_article_list a::attr(href)&#x27;</span>).getall()</span><br><span class="line">        name = <span class="number">00</span></span><br><span class="line">        <span class="keyword">for</span> link <span class="keyword">in</span> href:</span><br><span class="line">            <span class="built_in">print</span>(link)</span><br><span class="line">            name = name + <span class="number">1</span></span><br><span class="line">            <span class="comment"># 对文章的url地址发送请求</span></span><br><span class="line">            response = self.send_request(link)</span><br><span class="line">            <span class="keyword">if</span> response:</span><br><span class="line">                self.parse_detail(response, name)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">parse_detail</span>(<span class="params">self, response, name</span>):</span><br><span class="line">        html = response.text</span><br><span class="line">        <span class="comment"># print(html)</span></span><br><span class="line">        selector = parsel.Selector(html)</span><br><span class="line">        title = selector.css(<span class="string">&#x27;#articleContentId::text&#x27;</span>).get()</span><br><span class="line">        <span class="comment"># content = selector.css(&#x27;#content_views&#x27;).get()</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 由于这里使用parsel拿到的网页文件，打开会自动跳转到csdn首页，并且不能转为pdf，就想在这里用soup</span></span><br><span class="line">        soup = BeautifulSoup(html, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;debugdir/&#x27;</span> + <span class="built_in">str</span>(name) + <span class="string">&#x27;.css&#x27;</span>, <span class="string">&#x27;w&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            f.write(<span class="built_in">str</span>(soup))</span><br><span class="line">        <span class="comment">#print(soup)</span></span><br><span class="line">        content = soup.find(<span class="string">&#x27;div&#x27;</span>,<span class="built_in">id</span>=<span class="string">&quot;content_views&quot;</span>,class_=[<span class="string">&quot;markdown_views prism-atom-one-dark&quot;</span>, <span class="string">&quot;markdown_views prism-atom-one-light&quot;</span> , <span class="string">&quot;htmledit_views&quot;</span>]) <span class="comment">#class_=&quot;htmledit_views&quot;</span></span><br><span class="line">        <span class="built_in">print</span>(content)</span><br><span class="line">        <span class="comment"># print(content)</span></span><br><span class="line">        <span class="comment"># print(title, content)</span></span><br><span class="line">        html = html_str.<span class="built_in">format</span>(article=content)</span><br><span class="line">        self.write_content(html, title)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">write_content</span>(<span class="params">self, content, name</span>):</span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(name)</span><br><span class="line"></span><br><span class="line">        html_path = <span class="string">&quot;HTML/&quot;</span> + <span class="built_in">str</span>(self.change_title(name)) + <span class="string">&quot;.html&quot;</span></span><br><span class="line">        pdf_path =<span class="string">&quot;PDF/&quot;</span> + <span class="built_in">str</span>(self.change_title(name))+ <span class="string">&quot;.pdf&quot;</span></span><br><span class="line">        md_path = <span class="string">&quot;MD/&quot;</span> + <span class="built_in">str</span>(self.change_title(name)) + <span class="string">&quot;.md&quot;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 将内容保存为html文件</span></span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(html_path, <span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            f.write(content)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;正在保存&quot;</span>, name, <span class="string">&quot;.html&quot;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 将html文件转换成PDF文件</span></span><br><span class="line">        <span class="comment">#config = pdfkit.configuration(wkhtmltopdf=r&#x27;G:\Dev\wkhtmltopdf\bin\wkhtmltopdf.exe&#x27;)</span></span><br><span class="line">        <span class="comment">#pdfkit.from_file(html_path, pdf_path, configuration=config)</span></span><br><span class="line">        <span class="comment">#print(&quot;正在保存&quot;, name, &quot;.pdf&quot;)</span></span><br><span class="line">        <span class="comment"># os.remove(html_path)</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 将html文件转换成md格式</span></span><br><span class="line">        html_text = <span class="built_in">open</span>(html_path, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>).read()</span><br><span class="line">        markdown = html2text.html2text(html_text)</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(md_path, <span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> file:</span><br><span class="line">            file.write(markdown)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;正在保存&quot;</span>, name, <span class="string">&quot;.md&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">change_title</span>(<span class="params">self, title</span>):</span><br><span class="line">        mode = re.<span class="built_in">compile</span>(<span class="string">r&#x27;[\\\/\:\?\*\&quot;\&lt;\&gt;\|\!\t]&#x27;</span>)</span><br><span class="line">        new_title = re.sub(mode,<span class="string">&#x27;_&#x27;</span>, title)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> new_title</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">start</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="comment">#response = self.send_request(self.url)</span></span><br><span class="line">        <span class="comment">#if response:</span></span><br><span class="line">        <span class="comment">#    self.parse_content(response)</span></span><br><span class="line">        <span class="keyword">for</span> i, link <span class="keyword">in</span> <span class="built_in">enumerate</span>(self.lst):</span><br><span class="line">            <span class="built_in">print</span>(i + <span class="number">1</span>, link)</span><br><span class="line">            response = self.send_request(link)</span><br><span class="line">            <span class="keyword">if</span> response:</span><br><span class="line">                 self.parse_detail(response, i)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    csdn = CSDNSpider(lst)</span><br><span class="line">    csdn.start()</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="3效果"><a class="markdownIt-Anchor" href="#3效果"></a> 3.效果</h2>
<p>虽然得到了所有博客的md文件，整体效果稍微差一点，比如代码框没有代码类型，代码框中回车较多会被截成好几份等，但是也可以接受</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/05/06/%E7%88%AC%E5%8F%96%E8%87%AA%E5%B7%B1CSDN%E4%B8%8A%E6%89%80%E6%9C%89%E7%9A%84%E5%8D%9A%E5%AE%A2%E5%B9%B6%E8%BD%AC%E5%AD%98%E4%B8%BAmd%E6%96%87%E4%BB%B6/" data-id="cl2uhoedl005ke4j36ee38d2w" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E7%88%AC%E8%99%AB/" rel="tag">爬虫</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-hexo框架下博客应用夏娜主题并略微魔改" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/05/06/hexo%E6%A1%86%E6%9E%B6%E4%B8%8B%E5%8D%9A%E5%AE%A2%E5%BA%94%E7%94%A8%E5%A4%8F%E5%A8%9C%E4%B8%BB%E9%A2%98%E5%B9%B6%E7%95%A5%E5%BE%AE%E9%AD%94%E6%94%B9/" class="article-date">
  <time class="post-time" datetime="2022-05-06T03:29:57.000Z" itemprop="datePublished">
    <span class="post-month">5月</span><br/>
    <span class="post-day">06</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/05/06/hexo%E6%A1%86%E6%9E%B6%E4%B8%8B%E5%8D%9A%E5%AE%A2%E5%BA%94%E7%94%A8%E5%A4%8F%E5%A8%9C%E4%B8%BB%E9%A2%98%E5%B9%B6%E7%95%A5%E5%BE%AE%E9%AD%94%E6%94%B9/">hexo框架下博客应用夏娜主题并略微魔改</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/">教练我想学挂边躲牛</a>,<a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/">博客搭建</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="应用夏娜主题"><a class="markdownIt-Anchor" href="#应用夏娜主题"></a> 应用夏娜主题</h2>
<p><a target="_blank" rel="noopener" href="https://github.com/ShanaMaid/hexo-theme-shana">夏娜主题github连接</a></p>
<p><s>我直接进行一个主题README.md文件的搬</s></p>
<h3 id="安装"><a class="markdownIt-Anchor" href="#安装"></a> 安装</h3>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/ShanaMaid/hexo-theme-shana themes/shana</span><br></pre></td></tr></table></figure>
<h3 id="配置"><a class="markdownIt-Anchor" href="#配置"></a> 配置</h3>
<p>修改hexo根目录下的 <code>_config.yml</code></p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">language:</span> <span class="string">zh-CN</span></span><br><span class="line"></span><br><span class="line"><span class="attr">theme:</span> <span class="string">shana</span></span><br></pre></td></tr></table></figure>
<p>同时将<code>themes/shana/_source/</code>的<code>tags</code>和<code>categories</code>文件夹拷贝到hexo根目录下的<code>source</code>文件夹下</p>
<h3 id="更新"><a class="markdownIt-Anchor" href="#更新"></a> 更新</h3>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd themes/shana</span><br><span class="line"></span><br><span class="line">git pull origin master</span><br></pre></td></tr></table></figure>
<h2 id="瞎搞和魔改过程"><a class="markdownIt-Anchor" href="#瞎搞和魔改过程"></a> 瞎搞和魔改过程</h2>
<p><strong><s>由于我不是很懂前端，所以不能随意更改博客，只能走一步看一步</s></strong></p>
<h4 id="更改网站的favicon"><a class="markdownIt-Anchor" href="#更改网站的favicon"></a> 更改网站的favicon</h4>
<p>把你想改的图片放到hexo根目录下的source目录中，然后修改shana主图根目录下_config.yml配置文件favicon属性的值为图片名即可，如:</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">favicon:</span> <span class="string">/favicon.png</span></span><br></pre></td></tr></table></figure>
<h4 id="修改博客标题-副标题-描述-作者等内容"><a class="markdownIt-Anchor" href="#修改博客标题-副标题-描述-作者等内容"></a> 修改博客标题、副标题、描述、作者等内容</h4>
<p>在hexo根目录修改配置文件_config.yml对应内容即可。</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Site</span></span><br><span class="line"><span class="attr">title:</span> <span class="string">蓝湖畔的雨</span></span><br><span class="line"><span class="attr">subtitle:</span> <span class="string">&#x27;All tragedy erased, I see only wonders...&#x27;</span></span><br><span class="line"><span class="attr">description:</span> <span class="string">&#x27;&#x27;</span></span><br><span class="line"><span class="attr">keywords:</span></span><br><span class="line"><span class="attr">author:</span> <span class="string">StarsWhisper</span></span><br><span class="line"><span class="attr">language:</span> <span class="string">zh-CN</span></span><br><span class="line"><span class="attr">timezone:</span> <span class="string">&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
<h4 id="增加菜单"><a class="markdownIt-Anchor" href="#增加菜单"></a> 增加菜单</h4>
<p>一开始应该只有主页、归档、分类、标签四个菜单，可以通过修改shana主题根目录中的_config.yml配置文件来实现添加菜单</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">menu:</span></span><br><span class="line">  <span class="attr">Home:</span> <span class="string">/</span></span><br><span class="line">  <span class="attr">Archives:</span> <span class="string">/archives</span></span><br><span class="line">  <span class="attr">Categories:</span> <span class="string">/categories</span></span><br><span class="line">  <span class="attr">Tags:</span> <span class="string">/tags</span></span><br><span class="line">  <span class="attr">About:</span> <span class="string">/about</span></span><br><span class="line"><span class="attr">rss:</span> <span class="string">/atom.xml</span></span><br></pre></td></tr></table></figure>
<p>仿照 <strong>themes/shana/_source/</strong> 中的文件夹，建立about文件夹以及里面的内容，并把它扔到hexo根目录下的<code>source</code>文件夹下即可</p>
<p>若不在翻译文件中加入自定义的名称，自定义菜单选项会显示英文，所以我们在 <strong>\themes\shana\languages</strong> 目录下修改zh-CN.yml 添加如下内容</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#你的自定义名称: 中文翻译</span></span><br><span class="line"><span class="attr">About:</span> <span class="string">关于啊啊啊</span></span><br></pre></td></tr></table></figure>
<p>增加点击菜单后显示的内容，只需要在对应的index.md文件中增加内容即可</p>
<h4 id="增加或修改右键菜单内容"><a class="markdownIt-Anchor" href="#增加或修改右键菜单内容"></a> 增加或修改右键菜单内容</h4>
<p>在shana主题配置文件中找到gaymenu_item:，比如我的已经改成了</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">gaymenu_item:</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">公告</span></span><br><span class="line">  <span class="attr">url :</span> <span class="string">/announcement</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">标签</span></span><br><span class="line">  <span class="attr">url :</span> <span class="string">/tags</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">分类</span></span><br><span class="line">  <span class="attr">url :</span> <span class="string">/categories</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">归档</span></span><br><span class="line">  <span class="attr">url :</span> <span class="string">/archives</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">关于</span></span><br><span class="line">  <span class="attr">url :</span> <span class="string">/knightabout</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">传送门</span></span><br><span class="line">  <span class="attr">url :</span> <span class="string">/bridges</span></span><br></pre></td></tr></table></figure>
<p>仿照修改就可以</p>
<h4 id="修改右键菜单图标"><a class="markdownIt-Anchor" href="#修改右键菜单图标"></a> 修改右键菜单图标</h4>
<p>在 <strong>\themes\shana\source\plugin\galmenu</strong> 目录下修改img.png文件，自己P一下就行。</p>
<p>原图：</p>
<img src="/2022/05/06/hexo%E6%A1%86%E6%9E%B6%E4%B8%8B%E5%8D%9A%E5%AE%A2%E5%BA%94%E7%94%A8%E5%A4%8F%E5%A8%9C%E4%B8%BB%E9%A2%98%E5%B9%B6%E7%95%A5%E5%BE%AE%E9%AD%94%E6%94%B9/img1.png" class="" title="img1">
<p>修改后：</p>
<img src="/2022/05/06/hexo%E6%A1%86%E6%9E%B6%E4%B8%8B%E5%8D%9A%E5%AE%A2%E5%BA%94%E7%94%A8%E5%A4%8F%E5%A8%9C%E4%B8%BB%E9%A2%98%E5%B9%B6%E7%95%A5%E5%BE%AE%E9%AD%94%E6%94%B9/img.png" class="" title="img">
<h4 id="社交连接和友情连接"><a class="markdownIt-Anchor" href="#社交连接和友情连接"></a> 社交连接和友情连接</h4>
<p>修改shana配置文件， 比如:</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 社交链接</span></span><br><span class="line"><span class="attr">social:</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">url:</span> <span class="string">https://github.com/Wldcmzy</span></span><br><span class="line">  <span class="attr">name:</span> <span class="string">Github</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">url:</span> <span class="string">https://blog.csdn.net/wldcmzy</span></span><br><span class="line">  <span class="attr">name:</span> <span class="string">CSDN</span></span><br><span class="line"><span class="bullet">-</span> <span class="attr">url:</span> <span class="string">https://space.bilibili.com/83743701</span></span><br><span class="line">  <span class="attr">name:</span> <span class="string">bilibili(无技术和学习内容)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 友情链接</span></span><br><span class="line"><span class="comment"># name : url</span></span><br><span class="line"><span class="attr">link_title:</span> <span class="string">友情链接</span></span><br><span class="line"><span class="attr">links:</span></span><br><span class="line">  <span class="string">夏娜主题作者的博客:</span> <span class="string">https://shanamaid.github.io/</span></span><br></pre></td></tr></table></figure>
<h4 id="修改头像"><a class="markdownIt-Anchor" href="#修改头像"></a> 修改头像</h4>
<p>在shana配置文件中更改avatar属性的值为头像图片的url即可。</p>
<p>若想使用本地图片，则在 <strong>shana/source/</strong> 目录下创建images文件夹，并把头像图片扔到里面，avatar的值这样写:</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">avatar:</span> <span class="string">/images/avatar.png(这个是你的头像图片名)</span></span><br></pre></td></tr></table></figure>
<h4 id="修改背景图"><a class="markdownIt-Anchor" href="#修改背景图"></a> 修改背景图</h4>
<p>在 <strong>\shana\source\plugin\bganimation</strong> 目录下，直接把你想用的背景图片放进去。</p>
<p>然后修改文件bg.css</p>
<p>下方代码中对应的图片名部分改为自己的背景图片名，也可以使用图片对应的url</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.cb-slideshow</span> <span class="selector-tag">li</span><span class="selector-pseudo">:nth-child</span>(<span class="number">1</span>) <span class="selector-tag">span</span> &#123; <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&#x27;qianbei.jpg&#x27;</span>);&#125;</span><br><span class="line"><span class="selector-class">.cb-slideshow</span> <span class="selector-tag">li</span><span class="selector-pseudo">:nth-child</span>(<span class="number">2</span>) <span class="selector-tag">span</span> &#123; <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&#x27;sister.jpg&#x27;</span>);&#125;</span><br><span class="line"><span class="selector-class">.cb-slideshow</span> <span class="selector-tag">li</span><span class="selector-pseudo">:nth-child</span>(<span class="number">3</span>) <span class="selector-tag">span</span> &#123; <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&#x27;brother.jpg&#x27;</span>);&#125;</span><br><span class="line"><span class="selector-class">.cb-slideshow</span> <span class="selector-tag">li</span><span class="selector-pseudo">:nth-child</span>(<span class="number">4</span>) <span class="selector-tag">span</span> &#123; <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&#x27;shadow.jpg&#x27;</span>);&#125;</span><br><span class="line"><span class="selector-class">.cb-slideshow</span> <span class="selector-tag">li</span><span class="selector-pseudo">:nth-child</span>(<span class="number">5</span>) <span class="selector-tag">span</span> &#123; <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&#x27;knight.jpg&#x27;</span>);&#125;</span><br><span class="line"><span class="selector-class">.cb-slideshow</span> <span class="selector-tag">li</span><span class="selector-pseudo">:nth-child</span>(<span class="number">6</span>) <span class="selector-tag">span</span> &#123; <span class="attribute">background-image</span>: <span class="built_in">url</span>(<span class="string">&#x27;kuiruo.jpg&#x27;</span>);&#125;</span><br></pre></td></tr></table></figure>
<h4 id="修改鼠标指针"><a class="markdownIt-Anchor" href="#修改鼠标指针"></a> 修改鼠标指针</h4>
<p>在 <strong>\shana\source\css\images/</strong> 目录下修改icon.png文件，可以自己找图或者p一个或者画一个。喜欢小骑士的可以用我这个:</p>
<img src="/2022/05/06/hexo%E6%A1%86%E6%9E%B6%E4%B8%8B%E5%8D%9A%E5%AE%A2%E5%BA%94%E7%94%A8%E5%A4%8F%E5%A8%9C%E4%B8%BB%E9%A2%98%E5%B9%B6%E7%95%A5%E5%BE%AE%E9%AD%94%E6%94%B9/icon.png" class="" title="icon">
<h4 id="修改代码高亮-代码背景等"><a class="markdownIt-Anchor" href="#修改代码高亮-代码背景等"></a> 修改代码高亮、代码背景等</h4>
<p>对应文件 <strong>\shana\source\css\ _partial\highlight.styl</strong></p>
<p>修改代码背景: 修改下方代码中 <strong>background</strong> 的值</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$code</span>-block</span><br><span class="line">  <span class="attribute">border-left</span>: <span class="number">5px</span> solid <span class="number">#0000FF</span></span><br><span class="line">  <span class="attribute">background</span>: <span class="number">#2244AA</span></span><br><span class="line">  <span class="attribute">margin</span>: <span class="number">0</span> article-padding * -<span class="number">1</span></span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">15px</span> article-padding</span><br><span class="line">  <span class="attribute">overflow</span>: auto</span><br><span class="line">  <span class="attribute">color</span>: highlight-foreground</span><br><span class="line">  <span class="attribute">line-height</span>: font-size * line-height</span><br></pre></td></tr></table></figure>
<h4 id="修改文章版透明度"><a class="markdownIt-Anchor" href="#修改文章版透明度"></a> 修改文章版透明度</h4>
<p>修改文件 **\themes\shana\source\css\ _variables.styl ** 中color-background-rgba属性的第四个值</p>
<h4 id="更换换页按钮的文本"><a class="markdownIt-Anchor" href="#更换换页按钮的文本"></a> 更换换页按钮的文本</h4>
<p>修改文件 <strong>themes\shana\layout_partial\archive.ejs</strong> 中最下方对应内容</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;% if (page.total &gt; 1)&#123; %&gt;</span><br><span class="line">  &lt;nav id=&quot;page-nav&quot;&gt;</span><br><span class="line">    &lt;%- paginator(&#123;</span><br><span class="line">      prev_text: &quot;&lt;&lt;&lt; 上一页&quot;,</span><br><span class="line">      next_text: &quot;下一页 &gt;&gt;&gt;&quot;</span><br><span class="line">    &#125;) %&gt;</span><br><span class="line">  &lt;/nav&gt;</span><br><span class="line">&lt;% &#125; %&gt;</span><br></pre></td></tr></table></figure>
<h4 id="调宽博文显示栏"><a class="markdownIt-Anchor" href="#调宽博文显示栏"></a> 调宽博文显示栏</h4>
<p>打开文件 <strong>\themes\shana\source\css_partial\article.styl</strong> 在.artical下增加宽度内容</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.article</span></span><br><span class="line">  <span class="attribute">margin</span>: block-margin <span class="number">0</span></span><br><span class="line">  <span class="attribute">width</span>: <span class="number">115%</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/05/06/hexo%E6%A1%86%E6%9E%B6%E4%B8%8B%E5%8D%9A%E5%AE%A2%E5%BA%94%E7%94%A8%E5%A4%8F%E5%A8%9C%E4%B8%BB%E9%A2%98%E5%B9%B6%E7%95%A5%E5%BE%AE%E9%AD%94%E6%94%B9/" data-id="cl2uhoebj000ge4j3h0rc2gy4" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/web/" rel="tag">web</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" rel="tag">博客搭建</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-使用hexo框架发布博客" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/05/06/%E4%BD%BF%E7%94%A8hexo%E6%A1%86%E6%9E%B6%E5%8F%91%E5%B8%83%E5%8D%9A%E5%AE%A2/" class="article-date">
  <time class="post-time" datetime="2022-05-06T02:48:25.000Z" itemprop="datePublished">
    <span class="post-month">5月</span><br/>
    <span class="post-day">06</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/05/06/%E4%BD%BF%E7%94%A8hexo%E6%A1%86%E6%9E%B6%E5%8F%91%E5%B8%83%E5%8D%9A%E5%AE%A2/">hexo框架下博客的创建、编辑和发布</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/">教练我想学挂边躲牛</a>,<a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/">博客搭建</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="创建一篇博客"><a class="markdownIt-Anchor" href="#创建一篇博客"></a> 创建一篇博客</h2>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo new &quot;博客名称&quot;</span><br></pre></td></tr></table></figure>
<p>之后会提示你在特定的目录下创建了对应博客的md文件，以markdown格式编辑文件即可。</p>
<p>markdown格式基本语法不在本文记录范围之内。</p>
<h2 id="插入本地图片"><a class="markdownIt-Anchor" href="#插入本地图片"></a> 插入本地图片</h2>
<p>在hexo根目录的_config.yml文件中更改</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">post_asset_folder: true</span><br></pre></td></tr></table></figure>
<p>之后你使用 hexo new 命令创建新博客时，也会在同目录下创建同名文件夹</p>
<p>然后安装插件hexo-image-link</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install hexo-image-link --save</span><br></pre></td></tr></table></figure>
<p>在创建好的同名文件夹中放置图片，并在md文件中以相对路径引用即可，生成博客后可以看到图片</p>
<h2 id="使用数学公式"><a class="markdownIt-Anchor" href="#使用数学公式"></a> 使用数学公式</h2>
<p>在md文件中使用数学公式在网页中是体现不出来的，需要更换插件以及更改配置文件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm un hexo-renderer-marked --save</span><br><span class="line">npm i hexo-renderer-markdown-it-plus --save</span><br></pre></td></tr></table></figure>
<p>在hexo根目录下的_config.yml文件中添加内容</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">markdown_it_plus:</span><br><span class="line">  highlight: true</span><br><span class="line">  html: true</span><br><span class="line">  xhtmlOut: true</span><br><span class="line">  breaks: true</span><br><span class="line">  langPrefix:</span><br><span class="line">  linkify: true</span><br><span class="line">  typographer:</span><br><span class="line">  quotes: “”‘’</span><br><span class="line">  pre_class: highlight</span><br></pre></td></tr></table></figure>
<h2 id="标签与分类"><a class="markdownIt-Anchor" href="#标签与分类"></a> 标签与分类</h2>
<p>Front-matter 是文件最上方 <code>---</code> 之间的区域，可以通过修改Front-matter来添加标签与分类，比如本文为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">---</span><br><span class="line">title: 使用hexo框架发布博客</span><br><span class="line">date: 2022-05-06 10:48:25</span><br><span class="line">categories:</span><br><span class="line">  - [教练我想学挂边多牛, 博客搭建]</span><br><span class="line">tags:</span><br><span class="line">  - 博客搭建</span><br><span class="line">  - git</span><br><span class="line">  - web</span><br><span class="line">  - hexo</span><br><span class="line">---</span><br></pre></td></tr></table></figure>
<p>若是单级分类直接写分类名就行，多级分类需要像上面一样使用中括号</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/05/06/%E4%BD%BF%E7%94%A8hexo%E6%A1%86%E6%9E%B6%E5%8F%91%E5%B8%83%E5%8D%9A%E5%AE%A2/" data-id="cl2uhoede004we4j388txfoob" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/web/" rel="tag">web</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" rel="tag">博客搭建</a></li></ul>

    </footer>
  </div>
  
</article>




  
    <article id="post-windows环境零基础使用hexo框架+Github搭建个人博客" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/05/05/windows%E7%8E%AF%E5%A2%83%E9%9B%B6%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8hexo%E6%A1%86%E6%9E%B6+Github%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/" class="article-date">
  <time class="post-time" datetime="2022-05-05T13:05:27.000Z" itemprop="datePublished">
    <span class="post-month">5月</span><br/>
    <span class="post-day">05</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2022/05/05/windows%E7%8E%AF%E5%A2%83%E9%9B%B6%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8hexo%E6%A1%86%E6%9E%B6+Github%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/">windows环境零基础使用hexo框架+Github搭建个人博客</a>
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/">教练我想学挂边躲牛</a>,<a class="article-category-link" href="/categories/%E6%95%99%E7%BB%83%E6%88%91%E6%83%B3%E5%AD%A6%E6%8C%82%E8%BE%B9%E8%BA%B2%E7%89%9B/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/">博客搭建</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a class="markdownIt-Anchor" href="#介绍"></a> 介绍</h2>
<p><strong>Hexo</strong>是一个基于<strong>nodejs</strong>的<u><strong>静态</strong></u> <strong>(划重点)</strong> 博客网站生成器，可以让用户通过简单的操作快速生成一系列静态文件。</p>
<p>此外利用它搭建博客，还需要版本控制系统<strong>Git</strong></p>
<h2 id="前置"><a class="markdownIt-Anchor" href="#前置"></a> 前置</h2>
<h4 id="nodejs"><a class="markdownIt-Anchor" href="#nodejs"></a> Nodejs</h4>
<p>在<a target="_blank" rel="noopener" href="https://nodejs.org/zh-cn/">Nodejs官网</a>下载一个长期维护的稳定版本(LTS版本)后，无脑下一步安装即可</p>
<p>安装完成后，打开<strong>命令行</strong> <em>(按组合键win+r，在弹出窗口中输入cmd后按回车)</em>，分别输入以下语句，若成功显示版本号，则说明安装成功。其中<strong>npm</strong>是Nodejs包管理和分发工具。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">node -v</span><br><span class="line">npm -v</span><br></pre></td></tr></table></figure>
<h4 id="git"><a class="markdownIt-Anchor" href="#git"></a> Git</h4>
<p>从<a target="_blank" rel="noopener" href="https://git-scm.com/">Git官网</a>下载<strong>Git</strong>， 可以使用与上面相似的方法查看Git是否安装成功</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git --version</span><br></pre></td></tr></table></figure>
<h2 id="hexo的安装"><a class="markdownIt-Anchor" href="#hexo的安装"></a> Hexo的安装</h2>
<p>我们先找好或新建一个文件夹(最好是空文件夹)，当作储存博客信息的地方，并在这个文件夹中通过右键菜单打开<strong>Git Bash</strong>(与命令行类似)</p>
<p>在<strong>Git Bush</strong>中输入下方语句安装<strong>hexo</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install hexo-cli -g</span><br></pre></td></tr></table></figure>
<p>成功安装之后，它会反馈给你一个版本以及安装时间(至少我安装的时候是这样)，你也可以通过命令来查看版本明细</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo -v</span><br></pre></td></tr></table></figure>
<h2 id="生成静态文件并将网页服务在本地运行"><a class="markdownIt-Anchor" href="#生成静态文件并将网页服务在本地运行"></a> 生成静态文件并将网页服务在本地运行</h2>
<p>按顺序执行下方代码:</p>
<p>初始化操作，这一步操作后，生成网页的基础条件就达成了</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo init</span><br></pre></td></tr></table></figure>
<p>生成网页，由于现在还没有对相应文件做任何改动，所以生成默认的landscape主题网页</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo g</span><br></pre></td></tr></table></figure>
<p>在本地运行网页服务</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo s</span><br></pre></td></tr></table></figure>
<p>默认在你机器的4000端口开启网页服务(因为刚才的指令没有加别的参数)，可以在浏览器中输入localhost:4000查看</p>
<p>但是一旦你在<strong>Git Bush</strong>中关闭了服务，网页服务就会终止，若想让网页长久存在，需要把它发布到互联网(挂到github的服务器)上。</p>
<h2 id="把网页挂到互联网上"><a class="markdownIt-Anchor" href="#把网页挂到互联网上"></a> 把网页挂到互联网上</h2>
<h4 id="建立github仓库"><a class="markdownIt-Anchor" href="#建立github仓库"></a> 建立github仓库</h4>
<p>若没有github账号需要先注册。</p>
<p>新建一个名称为: <strong><a target="_blank" rel="noopener" href="http://xn--6qqv7i14ofosyrb.github.io">你的用户名.github.io</a></strong>的仓库，其他配置可以都不管。</p>
<p>建立仓库的方法是：</p>
<blockquote>
<p>1.点击右上角你的头像，在下拉框中点击<strong>Your repositories</strong></p>
<p>2.你能看到一个绿色的New，点击它创建一个新的仓库</p>
<p>3.仓库名必须要是&quot;<a target="_blank" rel="noopener" href="http://xn--6qqv7i14ofosyrb.github.io">你的用户名.github.io</a>&quot; 其他的不用管，直接点Create repositories创建仓库</p>
</blockquote>
<h4 id="生成ssh-key"><a class="markdownIt-Anchor" href="#生成ssh-key"></a> 生成SSH key</h4>
<p>先配置一些变量，这算是你的身份标识，在对应的地方填写你的用户名和注册邮箱</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name &quot;用户名&quot;</span><br><span class="line">git config --global user.email &quot;注册邮箱&quot;</span><br></pre></td></tr></table></figure>
<p>输入这两条语句可以查看这两个变量是否修改成功</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config user.name</span><br><span class="line">git config user.email</span><br></pre></td></tr></table></figure>
<p>然后生成ssh key 一直按回车就行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen -t rsa -C &quot;注册邮箱&quot;</span><br></pre></td></tr></table></figure>
<p>然后在你电脑的 <strong>C:\Users\你的windows用户名.ssh ** 目录下会生成key的信息， 其中Users目录的名字在你的电脑上可能汉化成了&quot;用户&quot;，若如此，你应该寻找</strong>C:\用户\你的windows用户名.ssh **目录</p>
<h4 id="把ssh-key添加到github"><a class="markdownIt-Anchor" href="#把ssh-key添加到github"></a> 把SSH key添加到Github</h4>
<p>我们需要在Github中新建SSH key</p>
<blockquote>
<p>1.点击右上角你的头像，在下拉菜单中点击<strong>Settings</strong></p>
<p>2.在左边找到<strong>SSH and GPG keys</strong>选项卡，点击</p>
<p>3.点击哪个绿油油的<strong>New SSH key</strong></p>
<p>4.你需要确认一件事，在.ssh目录中，后缀名为id_rsa.pub文件是公钥，没有后缀名的id_rsa文件是私钥，公钥是可以任何人知道的，而私钥你必须保密(感兴趣的去查询非对称密码体制，RAS算法)，所以你在下一步一定要保证你上传的是公钥</p>
<p>5.在Title中随便为SSH key取一个名字，用记事本打开.ssh目录中的公钥<strong>id_rsa.pub</strong>原封不动地复制粘贴到Key框中，开头结尾不要多加空格，你添加的东西和公钥内容完全相同。然后点击Add SHH key</p>
</blockquote>
<h4 id="将网页部署到github"><a class="markdownIt-Anchor" href="#将网页部署到github"></a> 将网页部署到Github</h4>
<p>打开你存放博客信息的文件夹，在刚才用hexo init命令初始化出来的文件中找到配置文件 <strong>_config.yml</strong> ，用记事本打开它，拉到最下面，修改以下信息，没有的就自己加上。(实际上repo对应的就是你仓库的连接)(我猜会不会跟&quot;种族主义&quot;有关，github现在建议你将master分支改成main，如果你改了的话，branch应该对应main而不是master，如果你没改就当我放P)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">deploy:</span><br><span class="line">  type: git</span><br><span class="line">  repo: https://github.com/你的Github用户名/你的Github用户名.github.io.git</span><br><span class="line">  branch: master</span><br></pre></td></tr></table></figure>
<p>在<strong>Git Bush</strong>输入以下命令，安装deploy-git</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install hexo-deployer-git --save</span><br></pre></td></tr></table></figure>
<p>然后输入命令</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hexo clean (清空hexo缓存(有时你更新了网页内容却发现没有更新，建议清空一下缓存))</span><br><span class="line">hexo generate (根据你当前的配置生成网页文件)</span><br><span class="line">hexo deploy (把网页部署到github服务器上)</span><br></pre></td></tr></table></figure>
<p>这三句可以简写为</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hexo clean</span><br><span class="line">hexo g</span><br><span class="line">hexo d</span><br></pre></td></tr></table></figure>
<p>你还记得hexo s是干嘛的吗?</p>
<p><strong>在浏览器中输入 你的用户名.github.io即可查看博客</strong></p>
<h2 id="注意"><a class="markdownIt-Anchor" href="#注意"></a> 注意</h2>
<p>1.国内网络访问Github可能非常不稳定，有时候传不上去，可以尝试多试几次或者搭个梯子。很长时间传不上去也无所谓，在本地测试好明天再传。</p>
<p>2.如果你改了东西在浏览器上体现不出来，不妨试一下ctrl+f5刷新浏览器缓存。</p>
<p>3.文件传到Github后，在服务器上部署需要花一段时间，可以在你仓库中点击Actions菜单查看，部署完成后，再访问才会显示修改后的结果。</p>
<h2 id="完结撒花"><a class="markdownIt-Anchor" href="#完结撒花"></a> 完结撒花</h2>
<p>关于如何发布博客、让博客应用其他的主题，以及想看我这个啥也不会的垃圾怎么摸索魔改博客的，欢迎来读我的其他文章</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/05/05/windows%E7%8E%AF%E5%A2%83%E9%9B%B6%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8hexo%E6%A1%86%E6%9E%B6+Github%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/" data-id="cl2uhoebr000te4j37f8zb76j" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/web/" rel="tag">web</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" rel="tag">博客搭建</a></li></ul>

    </footer>
  </div>
  
</article>




  


  <nav id="page-nav">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><span class="space">&hellip;</span><a class="page-number" href="/page/8/">8</a><a class="extend next" rel="next" href="/page/2/">下一页 &gt;&gt;&gt;</a>
  </nav>
</section>
        
          <aside id="sidebar">
  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <h1 class="blog-title">蓝湖畔淅淅沥沥的雨</h1>
    <h2 class="blog-subtitle">All tragedy erased, I see only wonders...</h2>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>



    <div class="social-link">
      
        <a class="hvr-bounce-in" href="https://github.com/Wldcmzy" target="_blank" title="Github">
          Github
        </a>
      
        <a class="hvr-bounce-in" href="https://blog.csdn.net/wldcmzy" target="_blank" title="CSDN">
          CSDN
        </a>
      
        <a class="hvr-bounce-in" href="https://space.bilibili.com/83743701" target="_blank" title="bilibili(无技术和学习内容)">
          bilibili(无技术和学习内容)
        </a>
      
    </div>

    <div class="friend-link">
      <h2>友情链接</h2>
      
        <a class="hvr-bounce-in" href="https://shanamaid.github.io/" target="_blank" title="夏娜主题作者的博客">
          夏娜主题作者的博客
        </a>
      
    </div>
  </div>
</div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy;2021 - 2022 StarsWhisper<br>
      由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动 | 
      主题-<a target="_blank" rel="noopener" href="https://github.com/ShanaMaid/hexo-theme-shana">Shana</a>(但是魔改)
      
    </div>
    
  </div>
</footer>
    </div>
    

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//apps.bdimg.com/libs/wow/0.1.6/wow.min.js"></script>
<script>
new WOW().init();
</script>   


  
<link rel="stylesheet" href="/plugin/fancybox/jquery.fancybox.css">

  
<script src="/plugin/fancybox/jquery.fancybox.pack.js"></script>




  
<link rel="stylesheet" href="/plugin/galmenu/GalMenu.css">

  
<script src="/plugin/galmenu/GalMenu.js"></script>

  <div class="GalMenu GalDropDown">
      <div class="circle" id="gal">
        <div class="ring">
          
            <a href="/announcement" title="" class="menuItem">公告</a>
          
            <a href="/tags" title="" class="menuItem">标签</a>
          
            <a href="/categories" title="" class="menuItem">分类</a>
          
            <a href="/archives" title="" class="menuItem">归档</a>
          
            <a href="/knightabout" title="" class="menuItem">关于</a>
          
            <a href="/bridges" title="" class="menuItem">传送门</a>
          
        </div>
        
          <audio id="audio" src="#"></audio>
        
      </div> 
</div>
<div id="overlay" style="opacity: 1; cursor: pointer;"></div>
  <script type="text/javascript">var items = document.querySelectorAll('.menuItem');
    for (var i = 0,
    l = items.length; i < l; i++) {
      items[i].style.left = (50 - 35 * Math.cos( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%";
      items[i].style.top = (50 + 35 * Math.sin( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%"
    }</script>
<script type="text/javascript">
  $(document).ready(function() {
    $('body').GalMenu({
      'menu': 'GalDropDown'
    })
  });
</script>

  <section class="hidden-xs"> 
  <ul class="cb-slideshow"> 
    <li><span>苟利</span></li> 
    <li><span>国家</span></li> 
    <li><span>生死以</span></li> 
    <li><span>岂能</span></li> 
    <li><span>祸福</span></li> 
    <li><span>趋避之</span></li> 
  </ul>
</section>

<script src="/js/script.js"></script>




  </div>
</body>
</html>